Skip to content

Commit

Permalink
Extend EntityPlusController and restore/rewrite `template_preprocess_…
Browse files Browse the repository at this point in the history
…paragraphs_item`

Fixed backdrop-contrib#113
  • Loading branch information
argiepiano committed Dec 7, 2021
1 parent 6428ffa commit e047525
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 32 deletions.
18 changes: 18 additions & 0 deletions ParagraphsItemEntityController.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* @file
* Entity Plus Controller implementation for the paragraphs entity.
*/

class ParagraphsItemEntityController extends EntityPlusController {

/**
* {@inheritdoc}
*/
public function buildContent($entity, $view_mode = 'full', $langcode = NULL, $content = array()) {
$build = parent::buildContent($entity, $view_mode, $langcode);
$build['#theme'] = 'paragraphs_item';
return $build;
}
}
34 changes: 2 additions & 32 deletions paragraphs.module
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function paragraphs_entity_info() {
'label callback' => '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,
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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',
Expand Down
62 changes: 62 additions & 0 deletions theme/paragraphs.theme.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit e047525

Please sign in to comment.