Commit eb09252b authored by jrockowitz's avatar jrockowitz

Setup block field.

parent 7f11a6c0
name: Block field
name: 'Block Field'
type: module
description: 'Provides a field that allows a content entity to create instances of configurable and custom blocks.'
package: Field
core: 8.x
<?php
namespace Drupal\block_field;
use Drupal\Core\Field\FieldItemInterface;
/**
* Defines an interface for the block field item.
*/
interface BlockFieldItemInterface extends FieldItemInterface {
}
<?php
namespace Drupal\block_field\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
/**
* Plugin implementation of the 'block_field' formatter.
*
* @FieldFormatter(
* id = "block_field",
* label = @Translation("Block field"),
* field_types = {
* "block_field"
* }
* )
*/
class BlockFieldFormatter extends FormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$elements = array();
foreach ($items as $delta => $item) {
// @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);
$elements[$delta] = [
'#theme' => 'block',
'#attributes' => [],
'#configuration' => $block_instance->getConfiguration(),
'#plugin_id' => $block_instance->getPluginId(),
'#base_plugin_id' => $block_instance->getBaseId(),
'#derivative_plugin_id' => $block_instance->getDerivativeId(),
'#id' => $item->id,
'content' => $block_instance->build(),
];
}
return $elements;
}
}
<?php
namespace Drupal\block_field\Plugin\Field\FieldType;
use Drupal\block\BlockInterface;
use Drupal\block_field\BlockFieldItemInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
/**
* Plugin implementation of the 'block_field' field type.
*
* @FieldType(
* id = "block_field",
* label = @Translation("Block (plugin)"),
* description = @Translation("Stores an instance of a configurable or custom block."),
* category = @Translation("Reference"),
* default_widget = "block_field_default",
* default_formatter = "block_field",
* )
*/
class BlockFieldItem extends FieldItemBase implements BlockFieldItemInterface {
/**
* {@inheritdoc}
*/
public static function defaultFieldSettings() {
return array(
'id' => '',
'label' => '',
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'configuration' => [],
) + parent::defaultFieldSettings();
}
/**
* {@inheritdoc}
*/
public static function mainPropertyName() {
return 'id';
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['id'] = DataDefinition::create('string')
->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'));
return $properties;
}
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return array(
'columns' => array(
'id' => array(
'description' => 'The block plugin id',
'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',
'size' => 'big',
'serialize' => TRUE,
),
),
);
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('id')->getValue();
return $value === NULL || $value === '';
}
/**
* {@inheritdoc}
*/
public function setValue($values, $notify = TRUE) {
// Treat the values as property value of the main property, if no array is
// given.
if (isset($values) && !is_array($values)) {
$values = [static::mainPropertyName() => $values];
}
if (isset($values)) {
$values += [
'configuration' => [],
];
}
// Unserialize the values.
if (is_string($values['configuration'])) {
$values['configuration'] = unserialize($values['configuration']);
}
parent::setValue($values, $notify);
}
}
<?php
namespace Drupal\block_field\Plugin\Field\FieldWidget;
use Drupal\block\BlockInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'block_field' widget.
*
* @FieldWidget(
* id = "block_field_default",
* label = @Translation("Block field"),
* field_types = {
* "block_field"
* }
* )
*/
class BlockFieldWidget extends WidgetBase {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return array(
'id' => '',
'label' => '',
'label_display' => BlockInterface::BLOCK_LABEL_VISIBLE,
'configuration' => [],
) + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
/** @var \Drupal\block_field\BlockFieldItemInterface $item */
$item = $items[$delta];
/** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
$block_manager = \Drupal::service('plugin.manager.block');
$options = [];
$definitions = $block_manager->getSortedDefinitions();
foreach ($definitions as $id => $definition) {
$category = (string) $definition['category'];
$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'),
'#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,
);
return $element;
}
/**
* Validate.
*/
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;
}
}
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