Skip to content

Instantly share code, notes, and snippets.

View lincoln-chawora's full-sized avatar

Lincoln Chawora lincoln-chawora

  • TPX Impact
View GitHub Profile
@lincoln-chawora
lincoln-chawora / node.php
Created November 25, 2019 11:42
How to render a template file in drupal 7
<?php print theme_render_template(drupal_get_path('theme', 'theme_name') . '/templates/theme/prompt-popup.tpl.php', array('prompt_paragraph' => $content['field_prompt_paragraph_reference'])); ?>
@lincoln-chawora
lincoln-chawora / variable_get()_in_D7.php
Last active November 14, 2019 13:36
How to use variable_get() to get the value of a form field
// $cancel_policy will equal the value of the field "field_name"
$cancel_policy = variable_get('field_name', '');
@lincoln-chawora
lincoln-chawora / exclude_current_node_from_view.php
Last active October 14, 2019 10:12
How to exclude the current node from a view display in drupal 8
function YOUR_MODULE_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
if ($view->id() == 'name_of_your_view') {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof NodeInterface) {
// You can get nid and anything else you need from the node object.
$nid = $node->id();
$query
->addWhere(0, db_and()
->condition('node_field_data.nid', $nid, '!='));
@lincoln-chawora
lincoln-chawora / delete_field_using_hook_n_in_D8.php
Last active October 4, 2019 16:23
How to delete a field from a paragraph in drupal 8 using hook n update, this function would go inside the .install file of you module.
function YOUR_MODULE_NAME_update_8001(&$sandbox) {
FieldStorageConfig::loadByName('paragraphs_type', 'example_field_name')->delete();
FieldConfig::loadByName('paragraph', 'example_paragraph_name', 'example_field_name')->delete();
}
@lincoln-chawora
lincoln-chawora / RouteSubscriber.php
Last active September 24, 2019 16:47
Route subscriber to prevent users without the certain permission's or role from accessing pages (Webform admin pages example)
<?php
namespace Drupal\custom_module\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Listens to the dynamic route events.
*/
@lincoln-chawora
lincoln-chawora / add_class_to_link_through_field_preprocessor.php
Created September 12, 2019 23:34
How to add (class and data) attributes to a link through field preprocessor in drupal 7
@lincoln-chawora
lincoln-chawora / add_classes_to_views_rows.php
Created September 11, 2019 14:01
How to add classes to views rows programmatically in drupal 7
/**
* Implements hook_views_view_unformatted().
*/
function YOUR_THEME_preprocess_views_view_unformatted(&$vars) {
$results = $vars['view']->result;
$iteration = 0;
foreach ($results as $key => $result) {
if (empty($result->field_image)) {
$vars['classes_array'][$key] .= ' empty__' . ($iteration % 5 + 1);
@lincoln-chawora
lincoln-chawora / delete_field_instance_using_hook_ni_D7.php
Last active October 4, 2019 16:18
How to delete a field instance in drupal 7 using hook n update
/**
* Delete field instance field_journey_image_type.
*/
function YOUR_THEME_journey_module_video_update_7001() {
$field_journey_image_type = field_info_instance('paragraphs_item', 'field_journey_image_type', 'journey_module_video');
if (!is_null($field_journey_image_type)) {
field_delete_instance($field_journey_image_type);
field_purge_batch(1);
}
@lincoln-chawora
lincoln-chawora / update_main_menu_using_hook_n_update.php
Last active September 4, 2019 16:29
How to update a menu link in drupal 7 using hook n update
/**
* Update we chat link inside menu social.
*/
function YOUR_THEME_main_menu_update_7004() {
$id = db_select('menu_links', 'm')
->fields('m', ['mlid'])
->condition('menu_name', 'menu-social')
->condition('link_title', 'WeChat')
->execute()
->fetchCol();
@lincoln-chawora
lincoln-chawora / resize_window_size.js
Created September 2, 2019 16:38
How to make changes on the window resize using the jquery-smartresize library
$(window).on('debouncedresize', function() {
var win = $(this);
if (win.width() > 768) {
$footerMenuContent.find('li:hidden').show();
} else {
$footerMenuContent.find('> .menu > li:not(.first)').hide();
}
});