From e0475255a2062ecfb5088243dd88c46d68bfda1a Mon Sep 17 00:00:00 2001 From: argiepiano Date: Mon, 6 Dec 2021 18:06:00 -0700 Subject: [PATCH] Extend EntityPlusController and restore/rewrite `template_preprocess_paragraphs_item` Fixed #113 --- ParagraphsItemEntityController.inc | 18 +++++++++ paragraphs.module | 34 +--------------- theme/paragraphs.theme.inc | 62 ++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 ParagraphsItemEntityController.inc diff --git a/ParagraphsItemEntityController.inc b/ParagraphsItemEntityController.inc new file mode 100644 index 0000000..37661a6 --- /dev/null +++ b/ParagraphsItemEntityController.inc @@ -0,0 +1,18 @@ + 'entity_class_label', 'uri callback' => 'entity_class_uri', 'entity class' => 'ParagraphsItemEntity', - 'controller class' => 'EntityPlusController', + 'controller class' => 'ParagraphsItemEntityController', 'base table' => 'paragraphs_item', 'revision table' => 'paragraphs_item_revision', 'fieldable' => TRUE, @@ -1845,37 +1845,6 @@ function paragraphs_modal_admin_links($paragraphs_item) { return $admin_links; } -/** - * Implements hook_preprocess_HOOK(). - */ -function paragraphs_preprocess_entity_plus(&$variables) { - if ($variables['entity_type'] == 'paragraphs_item') { - $view_mode = $variables['elements']['#view_mode']; - $bundle = $variables['elements']['#bundle']; - $variables['view_mode'] = $view_mode; - $variables['bundle'] = $bundle; - $paragraph = $variables['paragraphs_item']; - $paragraph_id = $paragraph->item_id; - - // Add additional css classes for the paragraph item. - $variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $view_mode); - $variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $paragraph_id); - - // Check if modal admin is enabled and user has access before adding that functionality. - if (!paragraphs_item_access('delete', $paragraph) - || !paragraphs_item_access('create', $paragraph) - || !paragraphs_item_access('update', $paragraph) - || empty($variables['elements']['#modal_admin'])) { - return; - } - - $variables['classes_array'][] = backdrop_html_class('paragraphs-item-modal-admin'); - if (paragraphs_item_access('delete', $paragraph)) { - $variables['admin_links'][$paragraph_id] = paragraphs_modal_admin_links($paragraph); - } - } -} - /** * Retrieves contextual links for a path based on registered local tasks. * @@ -1978,6 +1947,7 @@ function paragraphs_autoload_info() { return array( 'ParagraphsItemEntity' => 'ParagraphsItemEntity.inc', 'ParagraphsItemMetadataController' => 'ParagraphsItemMetadataController.inc', + 'ParagraphsItemEntityController' => 'ParagraphsItemEntityController.inc', 'MigrateDestinationParagraphsItem' => 'migrate/destinations/MigrateDestinationParagraphsItem.inc', 'ParagraphsMigrateParagraphsFieldHandler' => 'migrate/fields/ParagraphsMigrateParagraphsFieldHandler.inc', 'paragraphs_handler_relationship' => 'views/paragraphs_handler_relationship.inc', diff --git a/theme/paragraphs.theme.inc b/theme/paragraphs.theme.inc index 43d2325..9dc3f69 100644 --- a/theme/paragraphs.theme.inc +++ b/theme/paragraphs.theme.inc @@ -23,3 +23,65 @@ function template_preprocess_paragraphs_items(&$variables, $hook) { $variables['theme_hook_suggestions'][] = 'paragraphs_items__' . $field_name; $variables['theme_hook_suggestions'][] = 'paragraphs_items__' . $field_name . '__' . $view_mode; } + +/** + * Process variables for paragraphs-item.tpl.php. + */ +function template_preprocess_paragraphs_item(&$variables, $hook) { + $variables['view_mode'] = $variables['elements']['#view_mode']; + $paragraphs_item_type = $variables['elements']['#entity_plus_type']; + $variables['entity_type'] = $paragraphs_item_type; + $paragraphs_item = $variables['elements']['#entity']; + $variables[$paragraphs_item_type] = $paragraphs_item; + + list(, , $bundle) = entity_extract_ids($paragraphs_item_type, $paragraphs_item); + $variables['bundle'] = $bundle; + + $paragraphs_item_id = entity_plus_id($paragraphs_item_type, $paragraphs_item); + + $variables['title'] = check_plain(entity_label($paragraphs_item_type, $paragraphs_item)); + + $uri = entity_uri($paragraphs_item_type, $paragraphs_item); + $variables['url'] = $uri && !empty($uri['path']) ? url($uri['path'], $uri['options']) : FALSE; + + if (isset($variables['elements']['#page'])) { + // If set by the caller, respect the page property. + $variables['page'] = $variables['elements']['#page']; + } + else { + // Else, try to automatically detect it. + $variables['page'] = $uri && !empty($uri['path']) && $uri['path'] == $_GET['q']; + } + + $variables['content'] = array(); + foreach (element_children($variables['elements']) as $key) { + $variables['content'][$key] = $variables['elements'][$key]; + } + // Make the field variables available with the appropriate language. + field_attach_preprocess($paragraphs_item_type, $paragraphs_item, $variables['content'], $variables); + + // Gather css classes. + $variables['classes_array'][] = backdrop_html_class('entity-' . $paragraphs_item_type); + $variables['classes_array'][] = backdrop_html_class($paragraphs_item_type . '-' . $bundle); + $variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $bundle); + $variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $variables['elements']['#view_mode']); + $variables['classes_array'][] = backdrop_html_class('paragraphs-item-' . $paragraphs_item_id); + + // Add suggestions. + $variables['theme_hook_suggestions'][] = $paragraphs_item_type; + $variables['theme_hook_suggestions'][] = $paragraphs_item_type . '__' . $bundle; + $variables['theme_hook_suggestions'][] = $paragraphs_item_type . '__' . $bundle . '__' . $variables['view_mode']; + $variables['theme_hook_suggestions'][] = $paragraphs_item_type . '__' . $paragraphs_item_id; + + // Add the modal css class if enabled + if (!paragraphs_item_access('delete', $paragraphs_item) + || !paragraphs_item_access('create', $paragraphs_item) + || !paragraphs_item_access('update', $paragraphs_item) + || empty($variables['elements']['#modal_admin'])) { + return; + } + $variables['classes_array'][] = backdrop_html_class('paragraphs-item-modal-admin'); + if (paragraphs_item_access('delete', $paragraphs_item)) { + $variables['admin_links'][$paragraphs_item_id] = paragraphs_modal_admin_links($paragraphs_item); + } +}