Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / Drupal 8 - Get canonical node path in Twig
Created October 19, 2018 16:45
Drupal 8 - Get canonical node path in Twig
{% set node_path = path('entity.node.canonical', { 'node': node.id }) %}
@DanLaufer
DanLaufer / Drupal 8 - get label of paragraph reference field from within paragraph entity in Twig
Last active October 19, 2018 16:44
Drupal 8 - get label of paragraph reference field from within paragraph entity in Twig
_context.elements['#paragraph']._referringItem.fielddefinition.label
@DanLaufer
DanLaufer / Drupal 8 - Cool Drush Commands
Last active April 1, 2019 19:19
Drupal 8 - Useful Drush Commands
// List things with Drush
Node Types - drush ev "print_r(array_keys(node_type_get_types()));"
User Roles - drush ev "print_r(array_keys(\Drupal::entityTypeManager()->getStorage('user_role')->loadMultiple()));"
Fields (on a content type) - drush ev "print_r(array_keys(\Drupal::entityManager()->getFieldDefinitions('node', 'insight')));"
Vocabularies - drush ev "print_r(array_keys(taxonomy_vocabulary_get_names()));"
Image Styles - drush ev "print_r(array_keys(\Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple()));"
// Running db updates for one module
drush ev "module_load_install('webform');webform_update_7423();"
@DanLaufer
DanLaufer / Drupal 8 - EntityQuery stuff
Last active November 6, 2021 12:37
Drupal 8 - EntityQuery stuff
// chaining with .entity.
$query = \Drupal::entityQuery('node');
$articles_by_name = $query->condition('type', 'article')
->condition('uid.entity.name', 'admin')
->execute();
//Equal/>=/<=/etc (3rd parameter)
// Find particular nodes published in the last year.
$query = \Drupal::entityQuery('node');
@DanLaufer
DanLaufer / Drupal 8 - EntityQuery to get Users with role programmatically
Created October 19, 2018 16:32
Drupal 8 - EntityQuery to get Users with role programmatically
// Find all users with the Administrator role.
$admin_user_ids = \Drupal::entityQuery('user')
->condition('roles', 'Administrator', 'CONTAINS')
->execute();
@DanLaufer
DanLaufer / Drupal 8 - Custom validation on entity creation
Created October 19, 2018 16:26
Drupal 8 - Custom validation on entity creation
//prevent 2 or more of the CTA paragraphs from being chosen in a paragraph reference field
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
$node = $form_state->getFormObject()->getEntity();
// Add validation to nodes by type
if(in_array($node->getType(),['campaign_page','case_study','event','insight','resource'])) {
$form['#validate'][] = '_mymodule_node_form_validate';
}
}
@DanLaufer
DanLaufer / Drupal 8 - Entity API CRUD
Last active February 23, 2019 18:14
Drupal 8 - Entity API CRUD
// CREATE
// Procedural Wrapper
$node = entity_create('node', array(
'title' => 'Make blogs great again',
'body' => 'This is where the body content goes. You could load text from a seperate file here too if you didn\'t want to have it all inline and have to worry about things like character escaping.',
));
// Static Create method
$node = Node::create(array('title' => 'First!'));
$node->save();
@DanLaufer
DanLaufer / Drupal 8 - Load entities with EntityTypeManager
Created October 19, 2018 16:17
Drupal 8 - Load entities with EntityTypeManager
// single
$service = \Drupal::service('entity_type.manager')->getStorage('node');
$node = $service->load(22);
// multiple
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->loadMultiple(array(22, 14, 8));
@DanLaufer
DanLaufer / Drupal 8 - Cool Drupal Console commands
Last active February 23, 2019 18:15
Drupal 8 - Cool Drupal Console commands
drupal generate:module
drupal generate:entity:content
drupal generate:entity:bundle
// create a module without having to answer questions
drupal generate:module \
--module="modulename" \
--machine-name="modulename" \
--module-path="/modules/custom" \
@DanLaufer
DanLaufer / Drupal 8 - Disable itok image settings in settings.php
Created October 19, 2018 15:07
Drupal 8 - Disable itok image settings in settings.php
$config[‘image.settings’][‘suppress_itok_output’] = TRUE;
$config[‘image.settings’][‘allow_insecure_derivatives’] = TRUE;