PluginHelperTrait.php in Layout Builder Restrictions 8.2
File
src/Traits/PluginHelperTrait.php
View source
<?php
namespace Drupal\layout_builder_restrictions\Traits;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\layout_builder\Context\LayoutBuilderContextTrait;
use Drupal\layout_builder\Entity\LayoutEntityDisplayInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
trait PluginHelperTrait {
use LayoutBuilderContextTrait;
use StringTranslationTrait;
protected function getBlockDefinitions(LayoutEntityDisplayInterface $display) {
if (method_exists($this
->sectionStorageManager(), 'load')) {
$section_storage = $this
->sectionStorageManager()
->load('defaults', [
'display' => EntityContext::fromEntity($display),
]);
}
else {
$section_storage = $this
->sectionStorageManager()
->loadEmpty('defaults')
->setSectionList($display);
}
$definitions = $this
->blockManager()
->getDefinitions();
$custom_blocks = [];
foreach ($definitions as $key => $definition) {
if ($definition['provider'] == 'block_content') {
$custom_blocks[] = $key;
}
}
$definitions = $this
->contextHandler()
->filterPluginDefinitionsByContexts($this
->getAvailableContexts($section_storage), $definitions);
$grouped_definitions = $this
->getDefinitionsByUntranslatedCategory($definitions);
foreach ($grouped_definitions as $category => $data) {
if (empty($data['definitions'])) {
unset($grouped_definitions[$category]);
}
foreach ($data['definitions'] as $key => $definition) {
if (in_array($key, $custom_blocks)) {
if (!isset($grouped_definitions['Custom blocks'])) {
$grouped_definitions['Custom blocks'] = [
'label' => 'Custom blocks',
'data' => [],
];
}
unset($grouped_definitions[$category]['definitions'][$key]);
$grouped_definitions['Custom blocks']['definitions'][$key] = $definition;
}
}
}
$custom_block_bundles = $this
->entityTypeBundleInfo()
->getBundleInfo('block_content');
if ($custom_block_bundles) {
$grouped_definitions['Custom block types'] = [
'label' => 'Custom block types',
'definitions' => [],
];
foreach ($custom_block_bundles as $machine_name => $value) {
$grouped_definitions['Custom block types']['definitions'][$machine_name] = [
'admin_label' => $value['label'],
'category' => $this
->t('Custom block types'),
];
}
}
ksort($grouped_definitions);
return $grouped_definitions;
}
protected function getDefinitionsByUntranslatedCategory(array $definitions) {
$definitions = $this
->getGroupedDefinitions($definitions, 'admin_label');
unset($definitions[$this
->t('Block')
->render()]['definitions']['broken']);
return $definitions;
}
public function getGroupedDefinitions(array $definitions = NULL, $label_key = 'label') {
$definitions = $this
->getSortedDefinitions($definitions, $label_key);
$grouped_definitions = [];
foreach ($definitions as $id => $definition) {
$category = $this
->getUntranslatedCategory($definition['category']);
if (!isset($grouped_definitions[$category])) {
$grouped_definitions[$category]['label'] = $category;
$grouped_definitions[$category]['translated_label'] = (string) $definition['category'];
}
$grouped_definitions[$category]['definitions'][$id] = $definition;
}
return $grouped_definitions;
}
public function categoryIsRestricted($category, array $allowed_block_categories) {
if (!empty($allowed_block_categories)) {
if (!in_array($category, $allowed_block_categories)) {
return TRUE;
}
}
return FALSE;
}
public function getUntranslatedCategory($category) {
if ($category instanceof TranslatableMarkup) {
$output = $category
->getUntranslatedString();
if ($output == '@entity fields') {
$output = 'Content fields';
}
if ($output == "Custom") {
$output = "Custom blocks";
}
}
else {
$output = (string) $category;
}
return $output;
}
protected function getSortedDefinitions(array $definitions = NULL, $label_key = 'label') {
uasort($definitions, function ($a, $b) use ($label_key) {
if ($a['category'] != $b['category']) {
return strnatcasecmp($a['category'], $b['category']);
}
return strnatcasecmp($a[$label_key], $b[$label_key]);
});
return $definitions;
}
public function getValuefromSectionStorage(array $section_storage, $requested_value) {
$section_storage = array_shift($section_storage);
$contexts = $section_storage
->getContexts();
$view_mode = 'default';
if ($requested_value == 'contexts') {
return $contexts;
}
if ($section_storage instanceof OverridesSectionStorageInterface) {
$entity = $contexts['entity']
->getContextValue();
$view_mode = $contexts['view_mode']
->getContextValue();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
}
elseif (isset($contexts['entity']) && $contexts['entity']
->getContextValue() instanceof ConfigEntityBase) {
$entity = $view_display = $contexts['entity']
->getContextValue();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
}
elseif (get_class($section_storage) == 'Drupal\\mini_layouts\\Plugin\\SectionStorage\\MiniLayoutSectionStorage') {
$view_display = $contexts['display']
->getContextValue();
}
elseif (isset($contexts['display'])) {
$entity = $contexts['display']
->getContextValue();
$view_mode = $entity
->getMode();
$bundle = $entity
->getTargetBundle();
$entity_type = $entity
->getTargetEntityTypeId();
}
elseif (isset($contexts['layout'])) {
$entity = $contexts['layout']
->getContextValue();
$bundle = $entity
->getTargetBundle();
$entity_type = $entity
->getTargetEntityType();
}
switch ($requested_value) {
case 'entity':
return $entity;
case 'view_mode':
return $view_mode;
case 'bundle':
return $bundle;
case 'entity_type':
return $entity_type;
}
$context = $entity_type . "." . $bundle . "." . $view_mode;
$storage = \Drupal::entityTypeManager()
->getStorage('entity_view_display');
if ($requested_value == 'storage') {
return $storage;
}
if (empty($view_display)) {
$view_display = $storage
->load($context);
}
if ($requested_value == 'view_display') {
return $view_display;
}
$third_party_settings = $view_display
->getThirdPartySetting('layout_builder_restrictions', 'entity_view_mode_restriction', []);
if ($requested_value == 'third_party_settings') {
return $third_party_settings;
}
return NULL;
}
public function getInlineBlockPlugins() {
$bundles = \Drupal::service('entity_type.bundle.info')
->getBundleInfo('block_content');
$inline_blocks = [];
foreach ($bundles as $machine_name => $bundle) {
$inline_blocks[] = 'inline_block:' . $machine_name;
}
return $inline_blocks;
}
protected function getLayoutDefinitions() {
return $this
->layoutManager()
->getFilteredDefinitions('layout_builder', []);
}
private function sectionStorageManager() {
return $this->sectionStorageManager ?? \Drupal::service('plugin.manager.layout_builder.section_storage');
}
private function blockManager() {
return $this->blockManager ?? \Drupal::service('plugin.manager.block');
}
private function layoutManager() {
return $this->layoutManager ?? \Drupal::service('plugin.manager.core.layout');
}
private function contextHandler() {
return $this->contextHandler ?? \Drupal::service('context.handler');
}
private function entityTypeBundleInfo() {
return $this->entityTypeBundleInfo ?? \Drupal::service('entity_type.bundle.info');
}
}