Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Proposition to optimize module. #11

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore IDE
.idea
43 changes: 20 additions & 23 deletions block_content_template.module
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use Drupal\Core\Render\Element;
*/
function block_content_template_theme() {
return [
'block_content_template' => [
'block_content' => [
'render element' => 'elements',
],
];
Expand All @@ -29,42 +29,39 @@ function block_content_template_block_content_view_alter(array &$build, EntityIn
// - the block entity has a view property
// - the block entity has a _referringItem property
if (isset($build['_layout_builder']) || isset($entity->view) || isset($entity->_referringItem)) {
$build['#theme'] = 'block_content_template';
$build['#theme'] = $entity->getEntityTypeId();
}
}

/**
* Preprocess function for block content template.
* Implements hook_preprocess_HOOK() for "block_content".
*/
function template_preprocess_block_content_template(&$variables) {
$variables['content'] = [];
if (isset($variables['elements']['_layout_builder'])) {
$variables['content'][] = $variables['elements']['_layout_builder'];
}
else {
$content = [];
foreach (Element::children($variables) as $key) {
$content[] = $variables[$key];
}
$variables['content'] = $content;
}
function template_preprocess_block_content(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['block_content'] = $variables['elements']['#block_content'];

$variables['label'] = $variables['block_content']->label();

// Remove quick edit as it doesn't make sense here. It also points to the view
// for instance, which doesn't make sense at all.
if (isset($variables['attributes']['data-quickedit-entity-id'])) {
unset($variables['attributes']['data-quickedit-entity-id']);
// Helpful $content variable for templates.
$variables += ['content' => []];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}

/**
* Implements hook_theme_suggestions_HOOK().
* Implements hook_theme_suggestions_HOOK() for "block_content".
*/
function block_content_template_theme_suggestions_block_content_template(array $variables) {
function block_content_template_theme_suggestions_block_content(array $variables) {
$suggestions = [];
$block_content = $variables['elements']['#block_content'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

$suggestions[] = 'block_content_template__' . $block_content->bundle();
$suggestions[] = 'block_content_template__' . $block_content->id();
$suggestions[] = 'block_content__' . $sanitized_view_mode;
$suggestions[] = 'block_content__' . $block_content->bundle();
$suggestions[] = 'block_content__' . $block_content->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'block_content__' . $block_content->id();
$suggestions[] = 'block_content__' . $block_content->id() . '__' . $sanitized_view_mode;

return $suggestions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#}
<div{{ attributes }}>
{{ title_prefix }}
{{ label }}
{{ title_suffix }}
{{ content }}
</div>