You are here

class Blocks in Block field 8

Provides a 'categories' BlockFieldSection.

Plugin annotation


@BlockFieldSelection(
  id = "blocks",
  label = @Translation("Blocks"),
)

Hierarchy

Expanded class hierarchy of Blocks

1 string reference to 'Blocks'
Blocks::buildConfigurationForm in src/Plugin/block_field/BlockFieldSelection/Blocks.php
Form constructor.

File

src/Plugin/block_field/BlockFieldSelection/Blocks.php, line 18

Namespace

Drupal\block_field\Plugin\block_field\BlockFieldSelection
View source
class Blocks extends BlockFieldSelectionBase {
  use DependencySerializationTrait;
  use StringTranslationTrait;

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'plugin_ids' => [],
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);

    /** @var \Drupal\block_field\BlockFieldManagerInterface $block_field_manager */
    $block_field_manager = \Drupal::service('block_field.manager');
    $definitions = $block_field_manager
      ->getBlockDefinitions();
    foreach ($definitions as $plugin_id => $definition) {
      $options[$plugin_id] = [
        [
          'category' => (string) $definition['category'],
        ],
        [
          'label' => $definition['admin_label'] . ' (' . $plugin_id . ')',
        ],
        [
          'provider' => $definition['provider'],
        ],
      ];
    }
    $default_value = !empty($this
      ->getConfiguration()['plugin_ids']) ? $this
      ->getConfiguration()['plugin_ids'] : array_keys($options);
    $form['blocks'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Blocks'),
      '#description' => $this
        ->t('Please select available blocks.'),
      '#open' => empty($this
        ->getConfiguration()['plugin_ids']),
      '#process' => [
        [
          $this,
          'formProcessMergeParent',
        ],
      ],
    ];
    $form['blocks']['plugin_ids'] = [
      '#type' => 'tableselect',
      '#header' => [
        $this
          ->t('Category'),
        $this
          ->t('Label/ID'),
        $this
          ->t('Provider'),
      ],
      '#options' => $options,
      '#js_select' => TRUE,
      '#required' => TRUE,
      '#empty' => $this
        ->t('No blocks are available.'),
      '#element_validate' => [
        [
          get_called_class(),
          'validatePluginIds',
        ],
      ],
      '#default_value' => array_combine($default_value, $default_value),
    ];
    return $form;
  }

  /**
   * Validates plugin_ids table select element.
   *
   * @param array $element
   *   A form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The $form_state from complete form.
   * @param array $complete_form
   *   Complete parent form.
   *
   * @return array
   *   Returns element with validated plugin ids.
   */
  public static function validatePluginIds(array &$element, FormStateInterface $form_state, array &$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;
  }

  /**
   * {@inheritdoc}
   */
  public function getReferenceableBlockDefinitions() {
    $block_field_manager = \Drupal::service('block_field.manager');
    $definitions = $block_field_manager
      ->getBlockDefinitions();
    $values = !empty($this
      ->getConfiguration()['plugin_ids']) ? $this
      ->getConfiguration()['plugin_ids'] : array_keys($definitions);
    $values = array_combine($values, $values);
    return array_intersect_key($definitions, $values);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockFieldSelectionBase::formProcessMergeParent public function Move settings up a level for easier processing and storage.
BlockFieldSelectionBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
BlockFieldSelectionBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
BlockFieldSelectionBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
BlockFieldSelectionBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
BlockFieldSelectionBase::__construct public function Constructs a new selection object. Overrides PluginBase::__construct
Blocks::buildConfigurationForm public function Form constructor. Overrides BlockFieldSelectionBase::buildConfigurationForm
Blocks::defaultConfiguration public function Gets default configuration for this plugin. Overrides BlockFieldSelectionBase::defaultConfiguration
Blocks::getReferenceableBlockDefinitions public function Returns filtered block definitions based on plugin settings. Overrides BlockFieldSelectionInterface::getReferenceableBlockDefinitions
Blocks::validatePluginIds public static function Validates plugin_ids table select element.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.