Skip to content

Instantly share code, notes, and snippets.

@shelane
Created May 10, 2021 22:29
Show Gist options
  • Save shelane/964b854cdb18904b2ab96aeeba660191 to your computer and use it in GitHub Desktop.
Save shelane/964b854cdb18904b2ab96aeeba660191 to your computer and use it in GitHub Desktop.
Basic code examples for Drupal 9
<?php
// Load a Node
use Drupal\node\Entity\Node;
$node = Node::load($nid);
// Load Multiple Nodes
use Drupal\node\Entity\Node;
$nodes = Node::loadMultiple($nids);
// Get the node from a request, previously menu_get_object
$node = Drupal::request()->attributes->get('node');
// Get the title of a node
use Drupal\node\Entity\Node;
$title = Node::load($nid)->get('title')->value;
// Get the Node Type
$node = Node::load($nid);
$node->getType();
// Get the value of a field from the node
$node->get('field_my_text’)->value; // textfield
$node->get('field_blog_url')->uri; // link
$node->get(‘body’)->summary; //body summary
/*
Depending on the structure of the field you will get the values in the right property, you can take a look to the field table definition to know what can you use after the get.
That is to get the first value of the field, if you have a multivalued field the things changes a little bit.
*/
// The method getValue() is used to get an array:
$array = $user->get('field_value')->getValue();
// If you want to get a skalar use:
$value = $user->get('field_value')->value;
/*
Both methods look similar, but do different things:
The method getValue() is to get the complete array of a field, which is quite useful for multiple value fields or multiple properties.
The method ->value gets the property with the same name. Most standard fields use value as the main property, other examples are target_id for entity references, lat / long for geofields, etc.
*/
// Render a node view, previously node_view
// For a single node
$node = Node::load($nid);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder($node->getEntityTypeId());
$node_view = $view_builder->viewMultiple($nodes, 'teaser');
// For multiple nodes at once
$nodes = Node::loadMultiple($nids);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder(reset($nodes)->getEntityTypeId());
$nodes_views = $view_builder->viewMultiple($nodes, 'teaser');
// In both cases you can call directly to render to get the html output or in twig just use
{{ node_view }}
You can still do node_view and render but that is calling to deprecated flagged methods, will change some day.
// Load a taxonomy term by the name
$terms = taxonomy_term_load_multiple_by_name(‘My Term’);
// Note: will return an array of matched terms.
// Get the term id
$tid = $term->get('tid')->value;
// Entity Query
$nids = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'blog_post')
->condition('field_blog_category', $tid)
->sort('nid', 'DESC')
->range(0, 10)
->execute();
// Get the alias of a path
$url_alias = \Drupal::service('path_alias.manager')->getAliasByPath('/taxonomy/term/111’);
// TWIG:
// Render the node title
{{ node.title.value }}
// Build the url of the node
{% set node_url = path('entity.node.canonical', {'node': node.id}) %}
// Or for other kinds of entities
// Link to the default frontpage content listing view:
<a href="{{ path('view.frontpage') }}">{{ 'View all content'|t }}</a>
// Link to a specific user profile page:
<a href="{{ path('entity.user.canonical', {'user': user.id}) }}">{{ 'View user profile'|t }}</a>
// Link to a view, and throw in some additional query string parameters:
<a href="{{ path('view.articles.page_1', {'page': 2}) }}">{{ 'Go to page 2'|t }}</a>
// Link to a view and pass in some arguments to the view:
<a href="{{ path('view.recent_articles_by_author.page_1', {'arg_0': user.field_display_name.value|drupal_escape }) }}">{{ 'See all the articles written by'|t }} {{ user.field_display_name.value }}</a>
// Get the current Path Url
$current_url = Url::fromRoute('<current>');
$path = $current_url->toString();
// Print the creation date formatted
{{ node.createdtime|format_date('long') }}
// Get the current user Object
use Drupal\user\Entity\User;
$userCurrent = \Drupal::currentUser();
$user = \Drupal\user\Entity\User::load($userCurrent->id());
$name = $user->getUsername();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment