annotate vendor/chi-teck/drupal-code-generator/templates/d8/render-element.twig @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\{{ machine_name }}\Element;
Chris@0 4
Chris@0 5 use Drupal\Core\Render\Element\RenderElement;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Provides a render element to display an entity.
Chris@0 9 *
Chris@0 10 * Properties:
Chris@0 11 * - #entity_type: The entity type.
Chris@0 12 * - #entity_id: The entity ID.
Chris@0 13 * - #view_mode: The view mode that should be used to render the entity.
Chris@0 14 * - #langcode: For which language the entity should be rendered.
Chris@0 15 *
Chris@0 16 * Usage Example:
Chris@0 17 * @code
Chris@0 18 * $build['node'] = [
Chris@0 19 * '#type' => 'entity',
Chris@0 20 * '#entity_type' => 'node',
Chris@0 21 * '#entity_id' => 1,
Chris@0 22 * '#view_mode' => 'teaser,
Chris@0 23 * '#langcode' => 'en',
Chris@0 24 * ];
Chris@0 25 * @endcode
Chris@0 26 *
Chris@0 27 * @RenderElement("entity")
Chris@0 28 */
Chris@0 29 class Entity extends RenderElement {
Chris@0 30
Chris@0 31 /**
Chris@0 32 * {@inheritdoc}
Chris@0 33 */
Chris@0 34 public function getInfo() {
Chris@0 35 return [
Chris@0 36 '#pre_render' => [
Chris@0 37 [get_class($this), 'preRenderEntityElement'],
Chris@0 38 ],
Chris@0 39 '#view_mode' => 'full',
Chris@0 40 '#langcode' => NULL,
Chris@0 41 ];
Chris@0 42 }
Chris@0 43
Chris@0 44 /**
Chris@0 45 * Entity element pre render callback.
Chris@0 46 *
Chris@0 47 * @param array $element
Chris@0 48 * An associative array containing the properties of the entity element.
Chris@0 49 *
Chris@0 50 * @return array
Chris@0 51 * The modified element.
Chris@0 52 */
Chris@0 53 public static function preRenderEntityElement(array $element) {
Chris@0 54
Chris@0 55 $entity_type_manager = \Drupal::entityTypeManager();
Chris@0 56
Chris@0 57 $entity = $entity_type_manager
Chris@0 58 ->getStorage($element['#entity_type'])
Chris@0 59 ->load($element['#entity_id']);
Chris@0 60
Chris@0 61 if ($entity && $entity->access('view')) {
Chris@0 62 $element['entity'] = $entity_type_manager
Chris@0 63 ->getViewBuilder($element['#entity_type'])
Chris@0 64 ->view($entity, $element['#view_mode'], $element['#langcode']);
Chris@0 65 }
Chris@0 66
Chris@0 67 return $element;
Chris@0 68 }
Chris@0 69
Chris@0 70 }