Commit b9e5994d authored by jrockowitz's avatar jrockowitz

Add support for selecting block plugin ids to BlockFieldItem.

parent e72136c7
......@@ -6,6 +6,7 @@ use Drupal\block\BlockInterface;
use Drupal\block_field\BlockFieldItemInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
......@@ -28,8 +29,7 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
*/
public static function defaultFieldSettings() {
return array(
'id' => '',
'settings' => [],
'plugin_ids' => [],
) + parent::defaultFieldSettings();
}
......@@ -75,6 +75,48 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
);
}
/**
* {@inheritdoc}
*/
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$field = $form_state->getFormObject()->getEntity();
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$options = [];
$definitions = $block_manager->getSortedDefinitions();
foreach ($definitions as $id => $definition) {
$options[$id] = [
['category' => (string) $definition['category']],
['label' => $definition['admin_label'] . ' (' . $id . ')'],
['provider' => $definition['provider']],
];
}
$default_value = $field->getSetting('plugin_ids') ?: array_keys($options);
$element = array();
$element['plugin_ids'] = array(
'#type' => 'tableselect',
'#title' => $this->t('Blocks'),
'#title_display' => 'invisible',
'#header' => [
'Category',
'Label/ID',
'Provider',
],
'#options' => $options,
'#js_select' => TRUE,
'#required' => TRUE,
'#empty' => t('No blocks are available.'),
'#element_validate' => [[get_called_class(), 'validatePluginIds']],
'#default_value' => array_combine($default_value, $default_value),
);
return $element;
}
/**
* {@inheritdoc}
*/
......@@ -104,4 +146,18 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
parent::setValue($values, $notify);
}
/**
* Validates plugin 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;
}
}
......@@ -45,12 +45,19 @@ class BlockFieldWidget extends WidgetBase {
$field_name = $this->fieldDefinition->getName();
$configuration_id = $field_name . '-' . $delta . '-settings';
$plugin_ids = $this->fieldDefinition->getSetting('plugin_ids');
$values = $form_state->getValues();
$plugin_id = (isset($values[$field_name][$delta]['id'])) ? $values[$field_name][$delta]['id'] : $item->id;
$settings = (!empty($values[$field_name][$delta]['settings'])) ? $values[$field_name][$delta]['settings'] : $item->settings ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
$options = [];
$definitions = $block_manager->getSortedDefinitions();
foreach ($definitions as $id => $definition) {
// If allowed plugin ids are set then check that this block should be
// included.
if ($plugin_ids && !isset($plugin_ids[$id])) {
continue;
}
$category = (string) $definition['category'];
$options[$category][$id] = $definition['admin_label'] . ' (' . $id . ')';
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment