diff --git a/eck.module b/eck.module index 9e256cf..c89d1e0 100644 --- a/eck.module +++ b/eck.module @@ -10,6 +10,7 @@ use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Entity\ContentEntityType; use Drupal\eck\EckEntityTypeBundleInfo; @@ -207,8 +208,9 @@ function eck_menu_local_actions_alter(&$local_actions) { */ function eck_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) { if ($entity instanceof \Drupal\eck\Entity\EckEntity) { + // Generalize the entity-type-specific defaults for easier default theming. $build['#theme'] = 'eck_entity'; - $build['#entity'] = $entity; + $build['#eck_entity'] = $entity; } } @@ -217,7 +219,7 @@ function eck_entity_view_alter(array &$build, EntityInterface $entity, EntityVie */ function eck_theme() { $templates['eck_entity'] = [ - 'render element' => 'entity', + 'render element' => 'elements', ]; $templates['eck_content_add_list'] = [ @@ -233,8 +235,8 @@ function eck_theme() { */ function eck_theme_suggestions_eck_entity(array $variables) { /** @var \Drupal\eck\Entity\EckEntity $entity */ - $entity = $variables['entity']['#entity']; - $sanitized_view_mode = strtr($variables['entity']['#view_mode'], '.', '_'); + $entity = $variables['elements']['#eck_entity']; + $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_'); $suggestions[] = 'eck_entity__' . $sanitized_view_mode; $suggestions[] = 'eck_entity__' . $entity->getEntityTypeId(); @@ -255,3 +257,19 @@ function eck_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entit unset($fields['title']); } } + +/** + * Implements template_preprocess_HOOK(). + */ +function template_preprocess_eck_entity(&$variables) { + $variables['eck_entity'] = $variables['elements']['#eck_entity']; + + $variables['entity_type'] = $variables['eck_entity']->getEntityTypeId(); + $variables['bundle'] = $variables['eck_entity']->bundle(); + + // Build the $content variable for templates. + $variables += ['content' => []]; + foreach (Element::children($variables['elements']) as $key) { + $variables['content'][$key] = $variables['elements'][$key]; + } +} diff --git a/templates/eck-entity.html.twig b/templates/eck-entity.html.twig index 1fc7943..567faed 100644 --- a/templates/eck-entity.html.twig +++ b/templates/eck-entity.html.twig @@ -1,3 +1,30 @@ -
- {{ entity }} +{# +/** + * @file + * Default theme implementation to display an ECK entity. + * + * Available variables: + * - eck_entity: The full ECK entity with limited access to object properties + * and methods. Only "getter" methods (method names starting with "get", + * "has", or "is") and a few common methods such as "id" and "label" are + * available. Calling other methods (such as node.delete) will result in an + * exception. + * - content: All ECK entity items. Use {{ content }} to print them all, + * or print a subset such as {{ content.field_example }}. Use + * {{ content|without('field_example') }} to temporarily suppress the printing + * of a given child element. + * - attributes: HTML attributes for the containing element. + * + * @see template_preprocess_eck_entity() + * + * @ingroup themeable + */ +#} +{% + set classes = [ + 'eck-entity', + ] +%} + + {{ content }}