|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Field-related hook implementations for the Emulsify theme. |
| 6 | + */ |
| 7 | + |
| 8 | +use Drupal\paragraphs\ParagraphInterface; |
| 9 | + |
| 10 | +/** |
| 11 | + * Implements hook_preprocess_field(). |
| 12 | + * |
| 13 | + * Adds a zero-based index to each paragraph item in an |
| 14 | + * entity_reference_revisions field referencing paragraphs. |
| 15 | + * |
| 16 | + * @param array $variables |
| 17 | + * Template variables array, containing: |
| 18 | + * - field_type: The field’s base type (e.g., 'entity_reference_revisions'). |
| 19 | + * - element: The render array for the field, with '#items'. |
| 20 | + * - items: An array of rendered items, each with ['content']['#paragraph']. |
| 21 | + */ |
| 22 | +function emulsify_preprocess_field(array &$variables) { |
| 23 | + // Only target entity_reference_revisions fields that point to paragraphs. |
| 24 | + if ( |
| 25 | + $variables['field_type'] === 'entity_reference_revisions' |
| 26 | + && $variables['element']['#items']->getItemDefinition()->getSetting('target_type') === 'paragraph' |
| 27 | + ) { |
| 28 | + $delta = 0; |
| 29 | + foreach ($variables['items'] as $key => &$item) { |
| 30 | + // Each item’s render array should have the paragraph object. |
| 31 | + if ( |
| 32 | + isset($item['content']['#paragraph']) |
| 33 | + && $item['content']['#paragraph'] instanceof ParagraphInterface |
| 34 | + ) { |
| 35 | + // Assign the index for use in Twig. |
| 36 | + $item['content']['#paragraph']->index = $delta++; |
| 37 | + } |
| 38 | + } |
| 39 | + unset($item); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Implements hook_theme_suggestions_field_alter(). |
| 45 | + * |
| 46 | + * Adds template suggestions for field renderings based on: |
| 47 | + * - Entity type. |
| 48 | + * - Field name. |
| 49 | + * - Bundle. |
| 50 | + * - View mode. |
| 51 | + * |
| 52 | + * Suggestions generated: |
| 53 | + * - field--[entity_type]--[field_name].html.twig |
| 54 | + * - field--[entity_type]--[field_name]--[bundle]--[view_mode].html.twig |
| 55 | + * |
| 56 | + * @param string[] $suggestions |
| 57 | + * Existing theme hook suggestions; we append ours. |
| 58 | + * @param array $variables |
| 59 | + * Template variables array, containing: |
| 60 | + * - element: The render array for the field, with metadata keys: |
| 61 | + * '#entity_type', '#field_name', '#bundle', '#view_mode'. |
| 62 | + * @param string $hook |
| 63 | + * The base hook name being invoked (should be 'field'). |
| 64 | + */ |
| 65 | +function emulsify_theme_suggestions_field_alter(array &$suggestions, array $variables, $hook) { |
| 66 | + // Only act on the 'field' hook. |
| 67 | + if ($hook !== 'field') { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Extract metadata from the render array. |
| 72 | + $entity_type = $variables['element']['#entity_type'] ?? ''; |
| 73 | + $field_name = $variables['element']['#field_name'] ?? ''; |
| 74 | + $bundle = $variables['element']['#bundle'] ?? ''; |
| 75 | + $view_mode = $variables['element']['#view_mode'] ?? ''; |
| 76 | + |
| 77 | + // Base suggestion: field--[entity_type]--[field_name].html.twig. |
| 78 | + if ($entity_type && $field_name) { |
| 79 | + $suggestions[] = "field__{$entity_type}__{$field_name}"; |
| 80 | + } |
| 81 | + |
| 82 | + // Detailed suggestion: field--[entity_type]--[field_name]--[bundle]--[view_mode].html.twig. |
| 83 | + if ($entity_type && $field_name && $bundle && $view_mode) { |
| 84 | + $suggestions[] = "field__{$entity_type}__{$field_name}__{$bundle}__{$view_mode}"; |
| 85 | + } |
| 86 | +} |
0 commit comments