Commit 0cfe6d8e authored by jrockowitz's avatar jrockowitz

Setup ajax callback for configuration form.

parent eb09252b
......@@ -27,14 +27,12 @@ class BlockFieldFormatter extends FormatterBase {
$elements = array();
foreach ($items as $delta => $item) {
$plugin_id = $item->id;
$configuration = $item->configuration;
// @see \Drupal\block\BlockViewBuilder::buildPreRenderableBlock
// @see template_preprocess_block()
$block_configuration = [
'label' => $item->label,
'label_display' => $item->label_display,
] + $item->configuration;
$block_instance = $block_manager->createInstance($item->id, $block_configuration);
$block_instance = $block_manager->createInstance($plugin_id, $configuration);
$elements[$delta] = [
'#theme' => 'block',
......
......@@ -29,8 +29,6 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
public static function defaultFieldSettings() {
return array(
'id' => '',
'label' => '',
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'configuration' => [],
) + parent::defaultFieldSettings();
}
......@@ -50,12 +48,6 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
->setLabel(t('ID'))
->setRequired(TRUE);
$properties['label'] = DataDefinition::create('string')
->setLabel(t('Label'));
$properties['label_display'] = DataDefinition::create('string')
->setLabel(t('Label display'));
$properties['configuration'] = MapDataDefinition::create()
->setLabel(t('Configuration'));
......@@ -73,16 +65,6 @@ class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
'type' => 'varchar',
'length' => 255,
),
'label' => array(
'description' => 'The block label.',
'type' => 'varchar',
'length' => 255,
),
'label_display' => array(
'description' => 'The block label display.',
'type' => 'varchar',
'length' => 50,
),
'configuration' => array(
'description' => 'Serialized array of configuration for the block.',
'type' => 'blob',
......
......@@ -3,6 +3,7 @@
namespace Drupal\block_field\Plugin\Field\FieldWidget;
use Drupal\block\BlockInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
......@@ -26,8 +27,6 @@ class BlockFieldWidget extends WidgetBase {
public static function defaultSettings() {
return array(
'id' => '',
'label' => '',
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'configuration' => [],
) + parent::defaultSettings();
}
......@@ -36,11 +35,18 @@ class BlockFieldWidget extends WidgetBase {
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
/** @var \Drupal\block_field\BlockFieldItemInterface $item */
$item = $items[$delta];
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$field_name = $this->fieldDefinition->getName();
$configuration_id = $field_name . '-' . $delta . '-configuration';
$values = $form_state->getValues();
$plugin_id = (isset($values[$field_name][$delta]['id'])) ? $values[$field_name][$delta]['id'] : $item->id;
$configuration = (!empty($values[$field_name][$delta]['configuration'])) ? $values[$field_name][$delta]['configuration'] : $item->configuration ?: ['label_display' => BlockInterface::BLOCK_LABEL_VISIBLE];
$options = [];
$definitions = $block_manager->getSortedDefinitions();
foreach ($definitions as $id => $definition) {
......@@ -48,45 +54,59 @@ class BlockFieldWidget extends WidgetBase {
$options[$category][$id] = $definition['admin_label'] . ' (' . $id . ')';
}
dsm($item);
// Validate element.
$element['#element_validate'] = array(
array($this, 'validate'),
);
$element['id'] = array(
'#type' => 'select',
'#title' => $this->t('ID'),
'#options' => $options,
'#empty_option' => $this->t('- None -'),
'#default_value' => $this->getSetting('id'),
'#default_value' => $plugin_id,
'#required' => $element['#required'],
);
$element['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Title'),
'#maxlength' => 255,
'#default_value' => $this->getSetting('label'),
);
$element['label_display'] = array(
'#type' => 'checkbox',
'#title' => $this->t('Display title'),
'#default_value' => ($this->getSetting('label_display') === BlockInterface::BLOCK_LABEL_VISIBLE),
'#return_value' => BlockInterface::BLOCK_LABEL_VISIBLE,
'#ajax' => [
'callback' => array($this, 'configurationForm'),
'wrapper' => $configuration_id,
],
);
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
// Build configuration container.
$element['configuration'] = [
'#type' => 'container',
'#attributes' => ['id' => $configuration_id],
'#tree' => TRUE,
];
// If block plugin exists get the block's configuration form.
if (!empty($plugin_id) && $block_manager->hasDefinition($plugin_id)) {
$block_instance = $block_manager->createInstance($plugin_id, $configuration);
$element['configuration'] += $block_instance->buildConfigurationForm([], $form_state);
// Hide admin label (aka description).
if (isset($element['configuration']['admin_label'])) {
$element['configuration']['admin_label']['#access'] = FALSE;
}
// DEBUG:
// $element['configuration']['#element_validate'] = array(array($this, 'validateConfiguration'));
}
return $element;
}
public function configurationForm(array $form, FormStateInterface $form_state) {
$trigger_element = $form_state->getTriggeringElement();
$array_parents = $trigger_element['#array_parents'];
$array_parents[(count($array_parents) - 1)] = 'configuration';
return NestedArray::getValue($form, $array_parents);
}
/**
* Validate.
* Form element validation handler for block configuration.
*/
public function validate(&$element, FormStateInterface $form_state) {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$id = $element['id']["#value"];
$definition = ($id) ? $block_manager->getDefinition($id) : NULL;
public static function validateConfiguration($element, FormStateInterface $form_state, $form) {
$values = $form_state->getValues();
$array_parents = $element['#parents'];
$configuration = NestedArray::getValue($values, $array_parents);
}
}
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