From aed556f56ce7ce164818791532cd5ebfd3cad91a Mon Sep 17 00:00:00 2001 From: kwiktorowicz Date: Fri, 1 Dec 2023 11:41:43 +0100 Subject: [PATCH 1/4] DROOP-746: d_block_field module - Fix recursive rendering error which blocks use to view the node. --- .../FieldFormatter/BlockFieldFormatter.php | 183 +++++++----------- 1 file changed, 69 insertions(+), 114 deletions(-) diff --git a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php index 063e89403..3aafca1d4 100644 --- a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php +++ b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php @@ -3,15 +3,13 @@ namespace Drupal\d_block_field\Plugin\Field\FieldFormatter; use Drupal\Component\Plugin\Exception\ContextException; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; -use Drupal\Core\Plugin\Context\ContextHandlerInterface; -use Drupal\Core\Plugin\Context\ContextRepositoryInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; -use Drupal\Core\Render\RendererInterface; -use Drupal\Core\Session\AccountInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList; +use Drupal\node\NodeInterface; +use Drupal\paragraphs\ParagraphInterface; /** * Plugin implementation of the 'd_block_field' formatter. @@ -26,108 +24,6 @@ */ class BlockFieldFormatter extends FormatterBase { - /** - * The context repository service. - * - * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface - */ - protected $contextRepository; - - /** - * The plugin context handler. - * - * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface - */ - protected $contextHandler; - - /** - * The renderer service. - * - * @var \Drupal\Core\Render\RendererInterface - */ - protected $renderer; - - /** - * The current user. - * - * @var \Drupal\Core\Session\AccountInterface - */ - protected $currentUser; - - /** - * Constructs a FormatterBase object. - * - * @param string $plugin_id - * The plugin_id for the formatter. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The definition of the field to which the formatter is associated. - * @param array $settings - * The formatter settings. - * @param string $label - * The formatter label display setting. - * @param string $view_mode - * The view mode. - * @param array $third_party_settings - * Any third party settings. - * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository - * The lazy context repository service. - * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler - * The ContextHandler for applying contexts to conditions properly. - * @param \Drupal\Core\Render\RendererInterface $renderer - * The renderer service. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user. - */ - public function __construct( - $plugin_id, - $plugin_definition, - FieldDefinitionInterface $field_definition, - array $settings, - $label, - $view_mode, - array $third_party_settings, - ContextRepositoryInterface $context_repository, - ContextHandlerInterface $context_handler, - RendererInterface $renderer, - AccountInterface $current_user - ) { - parent::__construct( - $plugin_id, - $plugin_definition, - $field_definition, - $settings, - $label, - $view_mode, - $third_party_settings - ); - - $this->contextRepository = $context_repository; - $this->contextHandler = $context_handler; - $this->renderer = $renderer; - $this->currentUser = $current_user; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $plugin_id, - $plugin_definition, - $configuration['field_definition'], - $configuration['settings'], - $configuration['label'], - $configuration['view_mode'], - $configuration['third_party_settings'], - $container->get('context.repository'), - $container->get('context.handler'), - $container->get('renderer'), - $container->get('current_user'), - ); - } - /** * {@inheritdoc} */ @@ -137,11 +33,10 @@ public function viewElements(FieldItemListInterface $items, $langcode) { /** @var \Drupal\d_block_field\BlockFieldItemInterface $item */ $block_instance = $item->getBlock(); - // Inject runtime contexts. if ($block_instance instanceof ContextAwarePluginInterface) { try { - $contexts = $this->contextRepository->getRuntimeContexts($block_instance->getContextMapping()); - $this->contextHandler->applyContextMapping($block_instance, $contexts); + $contexts = \Drupal::service('context.repository')->getRuntimeContexts($block_instance->getContextMapping()); + \Drupal::service('context.handler')->applyContextMapping($block_instance, $contexts); } catch (ContextException $e) { continue; @@ -149,7 +44,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } // Make sure the block exists and is accessible. - if (!$block_instance || !$block_instance->access($this->currentUser)) { + if (!$block_instance || !$block_instance->access(\Drupal::currentUser())) { continue; } @@ -162,12 +57,72 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#plugin_id' => $block_instance->getPluginId(), '#base_plugin_id' => $block_instance->getBaseId(), '#derivative_plugin_id' => $block_instance->getDerivativeId(), - 'content' => $block_instance->build(), + 'content' => $this->processBlockBuild($block_instance), ]; - $this->renderer->addCacheableDependency($elements[$delta], $block_instance); + /** @var \Drupal\Core\Render\RendererInterface $renderer */ + $renderer = \Drupal::service('renderer'); + $renderer->addCacheableDependency($elements[$delta], $block_instance); } return $elements; } + /** + * Get processed block build. + * + * @param BlockPluginInterface $block_instance + * The Block Plugin Interface. + * @return array + * The Block Build. + */ + private function processBlockBuild(BlockPluginInterface $block_instance): array { + $block_build = $block_instance->build(); + + // Get processed block build for entity view node block without recursive node rendering. + if ($block_instance->getBaseId() === 'entity_view') { + $node = $block_build['#node'] ?? NULL; + + if ($node instanceof NodeInterface) { + $sections = $node->hasField('field_page_section') && !$node->get('field_page_section')->isEmpty() + ? $node->get('field_page_section')->referencedEntities() + : NULL; + + if (!$sections) { + return $block_build; + } + + $block_build['content']['#node'] = $node->set( + 'field_page_section', + $this->getAllowedSectionsForEntityViewBlock($sections) + ); + } + } + + return $block_build; + } + + /** + * Get allowed references for page section field. + * + * @param array $sections + * All referenced entities from field page section. + * @return array + * Allowed referenced entities for field page section. + */ + private function getAllowedSectionsForEntityViewBlock(array $sections): array { + foreach ($sections as $key => $paragraph) { + if ($paragraph instanceof ParagraphInterface && $paragraph->getType() === 'd_p_block') { + $field_block_value = $paragraph->hasField('field_block') && !$paragraph->get('field_block')->isEmpty() + ? $paragraph->get('field_block')->getValue() + : NULL; + $field_block_value = reset($field_block_value); + + if (isset($field_block_value['plugin_id']) && $field_block_value['plugin_id'] === 'entity_view:node') { + unset($sections[$key]); + } + } + } + + return $sections; + } } From 19cc56146707b5c10a29fd7bd8128fd7112b8522 Mon Sep 17 00:00:00 2001 From: kwiktorowicz Date: Fri, 1 Dec 2023 13:43:24 +0100 Subject: [PATCH 2/4] DROOP-746: PHPCS --- .../FieldFormatter/BlockFieldFormatter.php | 127 ++++++++++++++++-- 1 file changed, 118 insertions(+), 9 deletions(-) diff --git a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php index 3aafca1d4..0934fc54a 100644 --- a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php +++ b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php @@ -4,12 +4,17 @@ use Drupal\Component\Plugin\Exception\ContextException; use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; +use Drupal\Core\Plugin\Context\ContextHandlerInterface; +use Drupal\Core\Plugin\Context\ContextRepositoryInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; -use Drupal\entity_reference_revisions\EntityReferenceRevisionsFieldItemList; use Drupal\node\NodeInterface; use Drupal\paragraphs\ParagraphInterface; +use Drupal\Core\Render\RendererInterface; +use Drupal\Core\Session\AccountInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin implementation of the 'd_block_field' formatter. @@ -24,6 +29,108 @@ */ class BlockFieldFormatter extends FormatterBase { + /** + * The context repository service. + * + * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface + */ + protected $contextRepository; + + /** + * The plugin context handler. + * + * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface + */ + protected $contextHandler; + + /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** + * Constructs a FormatterBase object. + * + * @param string $plugin_id + * The plugin_id for the formatter. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * The definition of the field to which the formatter is associated. + * @param array $settings + * The formatter settings. + * @param string $label + * The formatter label display setting. + * @param string $view_mode + * The view mode. + * @param array $third_party_settings + * Any third party settings. + * @param \Drupal\Core\Plugin\Context\ContextRepositoryInterface $context_repository + * The lazy context repository service. + * @param \Drupal\Core\Plugin\Context\ContextHandlerInterface $context_handler + * The ContextHandler for applying contexts to conditions properly. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer service. + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. + */ + public function __construct( + $plugin_id, + $plugin_definition, + FieldDefinitionInterface $field_definition, + array $settings, + $label, + $view_mode, + array $third_party_settings, + ContextRepositoryInterface $context_repository, + ContextHandlerInterface $context_handler, + RendererInterface $renderer, + AccountInterface $current_user + ) { + parent::__construct( + $plugin_id, + $plugin_definition, + $field_definition, + $settings, + $label, + $view_mode, + $third_party_settings + ); + + $this->contextRepository = $context_repository; + $this->contextHandler = $context_handler; + $this->renderer = $renderer; + $this->currentUser = $current_user; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $plugin_id, + $plugin_definition, + $configuration['field_definition'], + $configuration['settings'], + $configuration['label'], + $configuration['view_mode'], + $configuration['third_party_settings'], + $container->get('context.repository'), + $container->get('context.handler'), + $container->get('renderer'), + $container->get('current_user'), + ); + } + /** * {@inheritdoc} */ @@ -33,10 +140,11 @@ public function viewElements(FieldItemListInterface $items, $langcode) { /** @var \Drupal\d_block_field\BlockFieldItemInterface $item */ $block_instance = $item->getBlock(); + // Inject runtime contexts. if ($block_instance instanceof ContextAwarePluginInterface) { try { - $contexts = \Drupal::service('context.repository')->getRuntimeContexts($block_instance->getContextMapping()); - \Drupal::service('context.handler')->applyContextMapping($block_instance, $contexts); + $contexts = $this->contextRepository->getRuntimeContexts($block_instance->getContextMapping()); + $this->contextHandler->applyContextMapping($block_instance, $contexts); } catch (ContextException $e) { continue; @@ -44,7 +152,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { } // Make sure the block exists and is accessible. - if (!$block_instance || !$block_instance->access(\Drupal::currentUser())) { + if (!$block_instance || !$block_instance->access($this->currentUser)) { continue; } @@ -60,9 +168,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { 'content' => $this->processBlockBuild($block_instance), ]; - /** @var \Drupal\Core\Render\RendererInterface $renderer */ - $renderer = \Drupal::service('renderer'); - $renderer->addCacheableDependency($elements[$delta], $block_instance); + $this->renderer->addCacheableDependency($elements[$delta], $block_instance); } return $elements; } @@ -70,15 +176,16 @@ public function viewElements(FieldItemListInterface $items, $langcode) { /** * Get processed block build. * - * @param BlockPluginInterface $block_instance + * @param \Drupal\Core\Block\BlockPluginInterface $block_instance * The Block Plugin Interface. + * * @return array * The Block Build. */ private function processBlockBuild(BlockPluginInterface $block_instance): array { $block_build = $block_instance->build(); - // Get processed block build for entity view node block without recursive node rendering. + // Entity view node block without recursive node rendering. if ($block_instance->getBaseId() === 'entity_view') { $node = $block_build['#node'] ?? NULL; @@ -106,6 +213,7 @@ private function processBlockBuild(BlockPluginInterface $block_instance): array * * @param array $sections * All referenced entities from field page section. + * * @return array * Allowed referenced entities for field page section. */ @@ -125,4 +233,5 @@ private function getAllowedSectionsForEntityViewBlock(array $sections): array { return $sections; } + } From 46fafb9139374572f3f24b66422fb0eeaece8b99 Mon Sep 17 00:00:00 2001 From: Jakub Samulski Date: Fri, 12 Jan 2024 14:46:45 +0100 Subject: [PATCH 3/4] DROOP-746 Allowe to include/exclude plugin categories for the d_block_field --- ...rm_display.paragraph.d_p_block.default.yml | 30 ++++----- .../core.entity_view_mode.block.token.yml | 1 + ....field.paragraph.d_p_block.field_block.yml | 18 +++++- modules/custom/d_block/d_block.info.yml | 58 ++++++++--------- .../FieldFormatter/BlockFieldFormatter.php | 64 +------------------ .../Plugin/Field/FieldType/BlockFieldItem.php | 44 ++++++++----- .../Field/FieldWidget/BlockFieldWidget.php | 8 ++- 7 files changed, 100 insertions(+), 123 deletions(-) diff --git a/modules/custom/d_block/config/install/core.entity_form_display.paragraph.d_p_block.default.yml b/modules/custom/d_block/config/install/core.entity_form_display.paragraph.d_p_block.default.yml index 5e9bc9eae..9708ecad8 100644 --- a/modules/custom/d_block/config/install/core.entity_form_display.paragraph.d_p_block.default.yml +++ b/modules/custom/d_block/config/install/core.entity_form_display.paragraph.d_p_block.default.yml @@ -15,59 +15,61 @@ third_party_settings: children: - group_basic - group_settings + label: Tabs + region: content parent_name: '' weight: 0 format_type: tabs - region: content format_settings: - id: '' classes: '' + id: '' direction: horizontal - label: Tabs group_basic: children: - field_block + label: Basic + region: content parent_name: group_tabs weight: 5 format_type: tab - region: content format_settings: - id: '' classes: '' - description: '' + id: '' formatter: open + description: '' required_fields: true - label: Basic group_settings: children: - field_d_settings + label: Settings + region: content parent_name: group_tabs weight: 6 format_type: tab - region: content format_settings: - id: '' classes: '' - description: '' + id: '' formatter: closed + description: '' required_fields: true - label: Settings id: paragraph.d_p_block.default targetEntityType: paragraph bundle: d_p_block mode: default content: field_block: + type: d_block_field_default weight: 1 + region: content settings: plugin_id: '' settings: { } configuration_form: full third_party_settings: { } - type: d_block_field_default - region: content field_d_settings: + type: field_d_p_set_settings weight: 7 + region: content settings: filter_mode: '1' allowed_settings: @@ -121,8 +123,6 @@ content: column_count: status: 0 third_party_settings: { } - type: field_d_p_set_settings - region: content hidden: created: true status: true diff --git a/modules/custom/d_block/config/install/core.entity_view_mode.block.token.yml b/modules/custom/d_block/config/install/core.entity_view_mode.block.token.yml index 02287c299..19d05d8e7 100644 --- a/modules/custom/d_block/config/install/core.entity_view_mode.block.token.yml +++ b/modules/custom/d_block/config/install/core.entity_view_mode.block.token.yml @@ -5,5 +5,6 @@ dependencies: - block id: block.token label: Token +description: '' targetEntityType: block cache: true diff --git a/modules/custom/d_block/config/install/field.field.paragraph.d_p_block.field_block.yml b/modules/custom/d_block/config/install/field.field.paragraph.d_p_block.field_block.yml index dad15b2ab..210a18b5e 100644 --- a/modules/custom/d_block/config/install/field.field.paragraph.d_p_block.field_block.yml +++ b/modules/custom/d_block/config/install/field.field.paragraph.d_p_block.field_block.yml @@ -17,5 +17,21 @@ translatable: false default_value: { } default_value_callback: '' settings: - plugin_ids: { } + plugin_categories: + 'Chaos Tools': 'Chaos Tools' + Block: 0 + 'Content block': 0 + core: 0 + 'Droopler Social Media Block': 0 + 'Drupal 8 Mega Menu': 0 + Facets: 0 + Forms: 0 + Help: 0 + 'Lists (Views)': 0 + Menus: 0 + Search: 0 + System: 0 + User: 0 + Views: 0 + plugin_categories_exclude: 1 field_type: d_block_field diff --git a/modules/custom/d_block/d_block.info.yml b/modules/custom/d_block/d_block.info.yml index 361098ee6..b6c64a215 100644 --- a/modules/custom/d_block/d_block.info.yml +++ b/modules/custom/d_block/d_block.info.yml @@ -3,34 +3,34 @@ core_version_requirement: ^10 description: 'Provides "Block" paragraph and related configuration. It allows you to insert blocks into paragraphs.' type: module dependencies: - - droopler:d_block_field - - droopler:d_content - - droopler:d_p - - droopler:d_p_banner - - droopler:d_p_carousel - - droopler:d_p_counters - - droopler:d_p_form - - droopler:d_p_gallery - - droopler:d_p_reference_content - - droopler:d_p_side_by_side - - droopler:d_p_side_embed - - droopler:d_p_side_image - - droopler:d_p_side_tiles - - droopler:d_p_subscribe_file - - droopler:d_p_text_blocks - - droopler:d_p_text_paged - - droopler:d_p_text_with_bckg - - droopler:d_p_tiles - - droopler:d_social_media - - droopler:d_update - - drupal:block - - drupal:block_content - - drupal:field - - drupal:language - - drupal:system - - entity_reference_revisions:entity_reference_revisions - - paragraphs:paragraphs - -package: Droopler Paragraphs + - 'droopler:d_block_field' + - 'droopler:d_content' + - 'droopler:d_p' + - 'droopler:d_p_banner' + - 'droopler:d_p_carousel' + - 'droopler:d_p_counters' + - 'droopler:d_p_form' + - 'droopler:d_p_gallery' + - 'droopler:d_p_reference_content' + - 'droopler:d_p_side_by_side' + - 'droopler:d_p_side_embed' + - 'droopler:d_p_side_image' + - 'droopler:d_p_side_tiles' + - 'droopler:d_p_subscribe_file' + - 'droopler:d_p_text_blocks' + - 'droopler:d_p_text_paged' + - 'droopler:d_p_text_with_bckg' + - 'droopler:d_p_tiles' + - 'droopler:d_social_media' + - 'droopler:d_update' + - 'drupal:block' + - 'drupal:block_content' + - 'drupal:field' + - 'drupal:language' + - 'drupal:system' + - 'entity_reference_revisions:entity_reference_revisions' + - 'field_group:field_group' + - 'paragraphs:paragraphs' +package: 'Droopler Common' version: DROOPLER_VERSION core_incompatible: false diff --git a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php index 0934fc54a..0123cc78a 100644 --- a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php +++ b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php @@ -165,73 +165,13 @@ public function viewElements(FieldItemListInterface $items, $langcode) { '#plugin_id' => $block_instance->getPluginId(), '#base_plugin_id' => $block_instance->getBaseId(), '#derivative_plugin_id' => $block_instance->getDerivativeId(), - 'content' => $this->processBlockBuild($block_instance), + 'content' => $block_instance->build(), ]; $this->renderer->addCacheableDependency($elements[$delta], $block_instance); } - return $elements; - } - - /** - * Get processed block build. - * - * @param \Drupal\Core\Block\BlockPluginInterface $block_instance - * The Block Plugin Interface. - * - * @return array - * The Block Build. - */ - private function processBlockBuild(BlockPluginInterface $block_instance): array { - $block_build = $block_instance->build(); - // Entity view node block without recursive node rendering. - if ($block_instance->getBaseId() === 'entity_view') { - $node = $block_build['#node'] ?? NULL; - - if ($node instanceof NodeInterface) { - $sections = $node->hasField('field_page_section') && !$node->get('field_page_section')->isEmpty() - ? $node->get('field_page_section')->referencedEntities() - : NULL; - - if (!$sections) { - return $block_build; - } - - $block_build['content']['#node'] = $node->set( - 'field_page_section', - $this->getAllowedSectionsForEntityViewBlock($sections) - ); - } - } - - return $block_build; - } - - /** - * Get allowed references for page section field. - * - * @param array $sections - * All referenced entities from field page section. - * - * @return array - * Allowed referenced entities for field page section. - */ - private function getAllowedSectionsForEntityViewBlock(array $sections): array { - foreach ($sections as $key => $paragraph) { - if ($paragraph instanceof ParagraphInterface && $paragraph->getType() === 'd_p_block') { - $field_block_value = $paragraph->hasField('field_block') && !$paragraph->get('field_block')->isEmpty() - ? $paragraph->get('field_block')->getValue() - : NULL; - $field_block_value = reset($field_block_value); - - if (isset($field_block_value['plugin_id']) && $field_block_value['plugin_id'] === 'entity_view:node') { - unset($sections[$key]); - } - } - } - - return $sections; + return $elements; } } diff --git a/modules/custom/d_block_field/src/Plugin/Field/FieldType/BlockFieldItem.php b/modules/custom/d_block_field/src/Plugin/Field/FieldType/BlockFieldItem.php index 40c22eac6..89105ec3c 100644 --- a/modules/custom/d_block_field/src/Plugin/Field/FieldType/BlockFieldItem.php +++ b/modules/custom/d_block_field/src/Plugin/Field/FieldType/BlockFieldItem.php @@ -28,7 +28,8 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface { */ public static function defaultFieldSettings() { return [ - 'plugin_ids' => [], + 'plugin_categories' => [], + 'plugin_categories_exclude' => FALSE, ] + parent::defaultFieldSettings(); } @@ -75,6 +76,33 @@ public static function schema(FieldStorageDefinitionInterface $field_definition) ]; } + /** + * {@inheritdoc} + */ + public function fieldSettingsForm(array $form, FormStateInterface $form_state) { + $categories = \Drupal::service('plugin.manager.block')->getCategories(); + foreach ($categories as $category) { + $category_name = (string) $category; + $options[$category_name] = $category_name; + } + + $element['plugin_categories'] = [ + '#title' => $this->t('Plugins categories'), + '#description' => $this->t('Leave empty to allow all plugin categories.'), + '#type' => 'checkboxes', + '#options' => $options ?? [], + '#default_value' => $this->getSetting('plugin_categories'), + ]; + $element['plugin_categories_exclude'] = [ + '#title' => $this->t('Exclude selected categories'), + '#description' => $this->t('If unchecked, only plugins from selected categories will be available. If checked, plugins from selected categories will be excluded.'), + '#type' => 'checkbox', + '#default_value' => $this->getSetting('plugin_categories_exclude'), + ]; + + return $element; + } + /** * {@inheritdoc} */ @@ -136,18 +164,4 @@ public function getBlock() { return $block_instance; } - /** - * Validates plugin_ids table select element. - */ - public static function validatePluginIds(array &$element, FormStateInterface $form_state, &$complete_form) { - $value = array_filter($element['#value']); - if (array_keys($element['#options']) == array_keys($value)) { - $form_state->setValueForElement($element, []); - } - else { - $form_state->setValueForElement($element, $value); - } - return $element; - } - } diff --git a/modules/custom/d_block_field/src/Plugin/Field/FieldWidget/BlockFieldWidget.php b/modules/custom/d_block_field/src/Plugin/Field/FieldWidget/BlockFieldWidget.php index fc650f145..cb3a7c3d0 100644 --- a/modules/custom/d_block_field/src/Plugin/Field/FieldWidget/BlockFieldWidget.php +++ b/modules/custom/d_block_field/src/Plugin/Field/FieldWidget/BlockFieldWidget.php @@ -162,11 +162,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen $item->settings = $item->settings ?: []; } + $categories = array_filter($this->getFieldSetting('plugin_categories')); + $categories_exclude = $this->getFieldSetting('plugin_categories_exclude'); + $options = []; $definitions = $this->fieldManager->getBlockDefinitions(); foreach ($definitions as $id => $definition) { $category = (string) $definition['category']; - $options[$category][$id] = $definition['admin_label']; + + if (empty($categories) || ($categories_exclude xor in_array($category, $categories))) { + $options[$category][$id] = $definition['admin_label']; + } } // Make sure the plugin id is allowed, if not clear all settings. From 0faae58ee4487d2404a5c5f48e937ac595965a3f Mon Sep 17 00:00:00 2001 From: Jakub Samulski Date: Fri, 12 Jan 2024 14:50:09 +0100 Subject: [PATCH 4/4] DROOP-746 Allowe to include/exclude plugin categories for the d_block_field --- modules/custom/d_block/d_block.info.yml | 2 +- .../src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/custom/d_block/d_block.info.yml b/modules/custom/d_block/d_block.info.yml index b6c64a215..6a7a34a4a 100644 --- a/modules/custom/d_block/d_block.info.yml +++ b/modules/custom/d_block/d_block.info.yml @@ -31,6 +31,6 @@ dependencies: - 'entity_reference_revisions:entity_reference_revisions' - 'field_group:field_group' - 'paragraphs:paragraphs' -package: 'Droopler Common' +package: 'Droopler Paragraphs' version: DROOPLER_VERSION core_incompatible: false diff --git a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php index 0123cc78a..4a5b47eff 100644 --- a/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php +++ b/modules/custom/d_block_field/src/Plugin/Field/FieldFormatter/BlockFieldFormatter.php @@ -3,15 +3,12 @@ namespace Drupal\d_block_field\Plugin\Field\FieldFormatter; use Drupal\Component\Plugin\Exception\ContextException; -use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FormatterBase; use Drupal\Core\Plugin\Context\ContextHandlerInterface; use Drupal\Core\Plugin\Context\ContextRepositoryInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; -use Drupal\node\NodeInterface; -use Drupal\paragraphs\ParagraphInterface; use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface;