|
|
@ -5,6 +5,7 @@ namespace Drupal\twig_tweak; |
|
|
|
use Drupal\Core\Block\TitleBlockPluginInterface; |
|
|
|
use Drupal\Core\Block\TitleBlockPluginInterface; |
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
use Drupal\Core\Controller\TitleResolverInterface; |
|
|
|
use Drupal\Core\Controller\TitleResolverInterface; |
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityInterface; |
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
use Drupal\Core\Form\FormBuilderInterface; |
|
|
|
use Drupal\Core\Form\FormBuilderInterface; |
|
|
|
use Drupal\Core\Menu\MenuLinkTreeInterface; |
|
|
|
use Drupal\Core\Menu\MenuLinkTreeInterface; |
|
|
@ -176,8 +177,10 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function drupalBlock($id) { |
|
|
|
public function drupalBlock($id) { |
|
|
|
$block = $this->entityTypeManager->getStorage('block')->load($id); |
|
|
|
$block = $this->entityTypeManager->getStorage('block')->load($id); |
|
|
|
return $block ? |
|
|
|
if ($block && $this->entityAccess($block)) { |
|
|
|
$this->entityTypeManager->getViewBuilder('block')->view($block) : ''; |
|
|
|
return $this->entityTypeManager->getViewBuilder('block')->view($block); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -201,12 +204,10 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
$view_builder = $this->entityTypeManager->getViewBuilder('block'); |
|
|
|
$view_builder = $this->entityTypeManager->getViewBuilder('block'); |
|
|
|
|
|
|
|
|
|
|
|
$build = []; |
|
|
|
$build = []; |
|
|
|
|
|
|
|
|
|
|
|
/* @var $blocks \Drupal\block\BlockInterface[] */ |
|
|
|
/* @var $blocks \Drupal\block\BlockInterface[] */ |
|
|
|
foreach ($blocks as $id => $block) { |
|
|
|
foreach ($blocks as $id => $block) { |
|
|
|
// Should the block be displayed? (follow rules from block layout page). |
|
|
|
if ($this->entityAccess($block)) { |
|
|
|
if (!$block->access('view')) { |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
$block_plugin = $block->getPlugin(); |
|
|
|
$block_plugin = $block->getPlugin(); |
|
|
|
if ($block_plugin instanceof TitleBlockPluginInterface) { |
|
|
|
if ($block_plugin instanceof TitleBlockPluginInterface) { |
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
|
|
@ -216,6 +217,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
} |
|
|
|
} |
|
|
|
$build[$id] = $view_builder->view($block); |
|
|
|
$build[$id] = $view_builder->view($block); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return $build; |
|
|
|
return $build; |
|
|
|
} |
|
|
|
} |
|
|
@ -240,7 +242,7 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
$entity = $id ? |
|
|
|
$entity = $id ? |
|
|
|
$this->entityTypeManager->getStorage($entity_type)->load($id) : |
|
|
|
$this->entityTypeManager->getStorage($entity_type)->load($id) : |
|
|
|
$this->routeMatch->getParameter($entity_type); |
|
|
|
$this->routeMatch->getParameter($entity_type); |
|
|
|
if ($entity) { |
|
|
|
if ($entity && $this->entityAccess($entity)) { |
|
|
|
$render_controller = $this->entityTypeManager->getViewBuilder($entity_type); |
|
|
|
$render_controller = $this->entityTypeManager->getViewBuilder($entity_type); |
|
|
|
return $render_controller->view($entity, $view_mode, $langcode); |
|
|
|
return $render_controller->view($entity, $view_mode, $langcode); |
|
|
|
} |
|
|
|
} |
|
|
@ -265,15 +267,17 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
* A render array for the field or NULL if the value does not exist. |
|
|
|
* A render array for the field or NULL if the value does not exist. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) { |
|
|
|
public function drupalField($field_name, $entity_type, $id = NULL, $view_mode = 'default', $langcode = NULL) { |
|
|
|
$entity = $id ? |
|
|
|
$entity = $id |
|
|
|
$this->entityTypeManager->getStorage($entity_type)->load($id) : |
|
|
|
? $this->entityTypeManager->getStorage($entity_type)->load($id) |
|
|
|
$this->routeMatch->getParameter($entity_type); |
|
|
|
: $this->routeMatch->getParameter($entity_type); |
|
|
|
|
|
|
|
if ($entity && $this->entityAccess($entity)) { |
|
|
|
if ($langcode && $entity->hasTranslation($langcode)) { |
|
|
|
if ($langcode && $entity->hasTranslation($langcode)) { |
|
|
|
$entity = $entity->getTranslation($langcode); |
|
|
|
$entity = $entity->getTranslation($langcode); |
|
|
|
} |
|
|
|
} |
|
|
|
if (isset($entity->{$field_name})) { |
|
|
|
if (isset($entity->{$field_name})) { |
|
|
|
return $entity->{$field_name}->view($view_mode); |
|
|
|
return $entity->{$field_name}->view($view_mode); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return NULL; |
|
|
|
return NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -492,4 +496,21 @@ class TwigExtension extends \Twig_Extension { |
|
|
|
return $output; |
|
|
|
return $output; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Checks view access to a given entity. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
|
|
|
|
|
|
|
* Entity to check access. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return bool |
|
|
|
|
|
|
|
* The access check result. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @TODO Remove "check_access" option in 9.x. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
protected function entityAccess(EntityInterface $entity) { |
|
|
|
|
|
|
|
// Prior version 8.x-1.7 entity access was not checked. The "check_access" |
|
|
|
|
|
|
|
// option provides a workaround for possible BC issues. |
|
|
|
|
|
|
|
return !Settings::get('twig_tweak_check_access', TRUE) || $entity->access('view'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|