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 / paragraph_id_from_field_template.php
Created August 23, 2019 17:51
How to get paragraphs ID from inside a field template using a field preprocessor
$field['#object']->entity_id();
@lincoln-chawora
lincoln-chawora / load_node.php
Last active January 10, 2020 18:48
How load a node in drupal 8
$route_name = \Drupal::routeMatch()->getRouteName();
if ($route_name == 'entity.node.canonical') {
node = \Drupal::routeMatch()->getParameter('node');
$node_type = $node->getType();
$title = $node->getTitle();
$visible = $node->isPublished();
$node->setPromoted(TRUE);
$uid = $node->getOwnerId();
// $author will be user object of the person who created the node.
@lincoln-chawora
lincoln-chawora / menu_local_tasks_alter.php
Created September 2, 2019 12:03
How to remove a menu tab from a content type in drupal 8
/**
* Implements hook_menu_local_tasks_alter().
*
* Remove replicate menu tab.
*/
function YOUR_THEME_menu_local_tasks_alter(&$data, $route_name, RefinableCacheableDependencyInterface &$cacheability) {
$routes = &$data['tabs'][0];
if ($routes['replicate_ui.local_tasks:entity.node.replicate']) {
unset($routes['replicate_ui.local_tasks:entity.node.replicate']);
}
@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();
}
});
@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 / 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 / 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 / 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 / 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 / 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();
}