Skip to main content

Drupal 8

CheatSheets

Submitted by system on
Drupal 8 Entity API Cheat sheet Link: https://www.metaltoad.com/blog/drupal-8-entity-api-cheat-sheet Drupal Twig Cheat sheet Link: https://git.drupalcode.org/project/twig_tweak/-/blob/3.x/docs/cheat-sheet.md?plain=0 Drupal 8 Form API reference Link: https://drupalize.me/tutorial/form-element-reference?p=2766 -- Source https://cheatography.com/programming/ https://www.drupaleasy.com/blogs/ultimike/2022/08/are-cheatsheets-still-thing-drupal-developers

Export Configuration using Drupal Console into a Module

Submitted by system on
Drupal console can be used to export configuration to an existing module. Exporting Content Types : Remove UUID and Config hash. Also specify module as ji_custom in this case vendor/drupal/console/bin/drupal config:export:content:type support_ticket --remove-uuid --remove-config-hash --module=ji_custom
Exporting Custom Entity types, in this case generated using ECK module vendor/drupal/console/bin/drupal config:export:view internal_support_notes --remove-uuid --remove-config-hash --module=ji_custom

Writing Automated tests in Drupal 9

Submitted by system on
This is a start. Install PHP Unit: PHPUnit8.4+ for Drupal 9 composer require --dev phpunit/phpunit ^8.4
For the current portal, add core-dev option composer require 'drupal/core-dev:^9.5'
composer update

Drupal requires Prophecy PhpUnit when using PHPUnit 9 or greater. composer require --dev phpspec/prophecy-phpunit:^2'

Handling Issues in Migration

Submitted by system on
Fix "non-existent config entity name returned by FieldStorageConfigInterface::getBundles()" Normally it will show that a field belongs to a non existing bundle for a node, comment etcetra in log post migration. Solving a field that does not belong to non existent bundle in a node, example, field_agenda present in a non existent bundle makemeeting of node type $key_value_factory = \Drupal::service("keyvalue");
$field_map_kv_store = $key_value_factory->get("entity.definitions.bundle_field_map");
$node_map = $field_map_kv_store->get("node");

Rest API and JSON API

Submitted by system on
-- https://thebrainfiles.wearebrain.com/how-to-quickly-configure-drupal-as-a-decoupled-api-first-system-8730a3623388 https://documenter.getpostman.com/view/5459957/S1TZyFjw?version=latest#707772be-2d65-4b3a-aacc-ec3ddc9f2072 https://www.drupal.org/docs/8/modules/jsonapi/updating-existing-resources-patch https://www.drupal.org/docs/8/core/modules/jsonapi-module https://thewebtier.com/php/snippet-post-multiple-files-guzzlehttp-client/

Managing Flag Development In D8

Submitted by system on
Check if an entity has been flagged: /**@var \Drupal\flag\FlagService $flag_service */
$flag_service = \Drupal::service('flag');
$flag = $flag_service->getFlagById($flag_id);
return $flag->isFlagged($node);

Get all flagging users for an entity and flag: $flagging = $flag_service->getFlaggingUsers($entity, $flag); // $flag is optional
Flag an entity: $flag_service->flag($flag, $entity, $account); // $account is optional

Drupal 8 Node Operations

Submitted by system on
Check if field exists in a node <?php $entity->hasField('abc'); ?> Get value of a field: <?php use Drupal\node\Entity\Node $title = Node::load($nid)->get('title')->value; $node = \Drupal::entityManager()->getStorage('node')->load($nid); //Another way to load ?> For User Added fields: <?php $terms = $node->get('abc')->getValue(); $terms = $node->abc->getValue(); ?> Get Nid from URL <?php $nid = \Drupal::routeMatch()->getRawParameter($node_id); ?> -- Link https://www.frobiovox.com/posts/2016/03/28/simplify-drupal-8-field-value-calls.ht

Drupal 8 Theming

Submitted by system on
Templates are the default option for hook_theme: https://www.drupal.org/node/2231673 Advanced Theme Settings Change Record: https://www.drupal.org/node/2382645 Drupal 8 theming guide: https://www.drupal.org/theme-guide/8

API changes in Drupal 8 in comparison to Drupal 7

Submitted by system on
This post would reflect on few essential APIs in Drupal 7 and how they were changed in Drupal 8: drupal_get_title()
: Gets the title of the current page. Link to changelog here . Drupal 7: <?php $title = drupal_get_title(); ?> Drupal 8: <?php $request = \Drupal::request(); $route_match = \Drupal::routeMatch(); $title = \Drupal::service('title_resolver')->getTitle($request, $route_match->getRouteObject()); ?> -- Links: https://www.drupal.org/node/2067859