Skip to content

Instantly share code, notes, and snippets.

@avblink
Last active December 21, 2020 17:33
Show Gist options
  • Save avblink/6c0b996e47e1e80b50f9eaf133e3fb88 to your computer and use it in GitHub Desktop.
Save avblink/6c0b996e47e1e80b50f9eaf133e3fb88 to your computer and use it in GitHub Desktop.
Drupal8:Random

Install specific Drupal version with composer

composer create-project drupal-composer/drupal-project:8.x-dev some-dir --stability dev --no-interaction --no-install
cd some-dir
composer require --no-update drupal/core:^8.5
composer require --no-update --dev webflo/drupal-core-require-dev:^8.5
composer install [--no-dev]

Get current URI

$current_uri = Drupal::request()->getRequestUri();

Get request parameter

//http://example.com?reset=1
$reset = \Drupal::request()->get('reset');

Get current page node

$current_node = \Drupal::routeMatch()->getParameter('node');

Private temp store

if (\Drupal::currentUser()->isAnonymous()) {
  // Forces Drupal::service('user.private_tempstore') to work for anonymous user
  $_SESSION['forced'] = TRUE;
}

$tempstore = Drupal::service('user.private_tempstore')->get('store_name');
$temp_menu = $tempstore->get('stored_item_name');

$tempstore->delete('our_expertise_menu');
$tempstore->set('stored_item_name', ['foo', 'bar']);

Get paragraph field value

$node->field_presenter->first()->entity->field_persons_name->value;

Count node paragraphs in twig template

{{ node.field_paragraphs.getvalue | length }}

Get link field information

{{ entity['#entity'].field_link.get(0).title }}
{{ entity['#entity'].field_link.get(0).uri }}
{{ entity['#entity'].field_link.get(0).options }}
{{ link(entity['#entity'].field_link.get(0).title, entity['#entity'].field_link.get(0).uri, entity['#entity'].field_link.get(0).options) }}

Twig helper functions can be found here https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates

Nice article about how to create a field type with multiple fields https://ixis.co.uk/blog/drupal-8-creating-field-types-multiple-values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment