Categories.php in Block field 8
File
src/Plugin/block_field/BlockFieldSelection/Categories.php
View source
<?php
namespace Drupal\block_field\Plugin\block_field\BlockFieldSelection;
use Drupal\block_field\BlockFieldSelectionBase;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class Categories extends BlockFieldSelectionBase {
use DependencySerializationTrait;
use StringTranslationTrait;
public function defaultConfiguration() {
return [
'categories' => [],
] + parent::defaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$block_field_manager = \Drupal::service('block_field.manager');
$categories = $block_field_manager
->getBlockCategories();
$options = [];
foreach ($categories as $category) {
$category = (string) $category;
$options[$category] = $category;
}
$form['categories'] = [
'#type' => 'details',
'#title' => $this
->t('Categories'),
'#description' => $this
->t('Please select available categories.'),
'#open' => empty($this
->getConfiguration()['categories']),
'#process' => [
[
$this,
'formProcessMergeParent',
],
],
];
$default_value = !empty($this
->getConfiguration()['categories']) ? $this
->getConfiguration()['categories'] : array_keys($options);
$form['categories']['categories'] = [
'#type' => 'checkboxes',
'#header' => [
'Category',
],
'#options' => $options,
'#js_select' => TRUE,
'#empty' => $this
->t('No categories are available.'),
'#required' => TRUE,
'#default_value' => array_combine($default_value, $default_value),
];
return $form;
}
public function getReferenceableBlockDefinitions() {
$block_field_manager = \Drupal::service('block_field.manager');
$definitions = $block_field_manager
->getBlockDefinitions();
if (!empty($this
->getConfiguration()['categories'])) {
$categories = array_filter($this
->getConfiguration()['categories']);
$definitions = array_filter($definitions, function ($definition, $key) use ($categories) {
return isset($categories[(string) $definition['category']]);
}, ARRAY_FILTER_USE_BOTH);
}
return $definitions;
}
}
Classes
Name |
Description |
Categories |
Provides a 'categories' BlockFieldSection. |