class ExtraFieldBlockPlugin in Entity Extra Field 2.0.x
Same name and namespace in other branches
- 8 src/Plugin/ExtraFieldType/ExtraFieldBlockPlugin.php \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldBlockPlugin
Define extra field block type.
Plugin annotation
@ExtraFieldType(
id = "block",
label = @Translation("Block")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
- class \Drupal\entity_extra_field\Plugin\ExtraFieldType\ExtraFieldBlockPlugin uses EntityExtraFieldContextTrait
- class \Drupal\entity_extra_field\ExtraFieldTypePluginBase implements ExtraFieldTypePluginInterface uses PluginDependencyTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of ExtraFieldBlockPlugin
File
- src/
Plugin/ ExtraFieldType/ ExtraFieldBlockPlugin.php, line 35
Namespace
Drupal\entity_extra_field\Plugin\ExtraFieldTypeView source
class ExtraFieldBlockPlugin extends ExtraFieldTypePluginBase {
use EntityExtraFieldContextTrait;
/**
* @var \Drupal\Core\Session\AccountInterface
*/
private $currentUser;
/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $blockManager;
/**
* Extra field block plugin constructor.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin identifier.
* @param array $plugin_definition
* The plugin definition.
* @param \Drupal\Core\Utility\Token $token
* The token service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current logged in user object.
* @param \Drupal\Core\Routing\RouteMatchInterface $current_route_match
* The current route match service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* The entity field manager service.
* @param \Drupal\Core\Block\BlockManagerInterface $block_manager
* The block manager service.
*/
public function __construct(array $configuration, string $plugin_id, array $plugin_definition, Token $token, ModuleHandlerInterface $module_handler, AccountInterface $current_user, RouteMatchInterface $current_route_match, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, BlockManagerInterface $block_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $token, $module_handler, $current_route_match, $entity_type_manager, $entity_field_manager);
$this->currentUser = $current_user;
$this->blockManager = $block_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('token'), $container
->get('module_handler'), $container
->get('current_user'), $container
->get('current_route_match'), $container
->get('entity_type.manager'), $container
->get('entity_field.manager'), $container
->get('plugin.manager.block'), $container
->get('context.handler'), $container
->get('context.repository'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() : array {
return [
'block_type' => NULL,
'block_config' => [],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) : array {
$form = parent::buildConfigurationForm($form, $form_state);
/** @var \Drupal\Core\Entity\EntityFormInterface $form_object */
$form_object = $form_state
->getFormObject();
$entity = $form_object
->getEntity();
$form_state
->setTemporaryValue('gathered_contexts', [
'entity_extra_field.target_entity' => $entity
->getBaseEntityContext(),
] + $this
->getContextRepository()
->getAvailableContexts());
$block_type = $this
->getPluginFormStateValue('block_type', $form_state);
$form['block_type'] = [
'#type' => 'select',
'#title' => $this
->t('Block Type'),
'#required' => TRUE,
'#options' => $this
->getBlockTypeOptions(),
'#empty_option' => $this
->t('- Select -'),
'#ajax' => [
'event' => 'change',
'method' => 'replace',
] + $this
->extraFieldPluginAjax(),
'#default_value' => $block_type,
];
if (isset($block_type) && !empty($block_type) && $this->blockManager
->hasDefinition($block_type)) {
$block_config = $this
->getPluginFormStateValue('block_config', $form_state, []);
$block_instance = $this->blockManager
->createInstance($block_type, $block_config);
if ($block_instance instanceof PluginFormInterface) {
$form['block_config'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Block Configuration'),
'#tree' => TRUE,
];
$subform = [
'#parents' => array_merge($form['#parents'], [
'block_config',
]),
];
$form['block_config'] += $block_instance
->buildConfigurationForm($subform, SubformState::createForSubform($subform, $form, $form_state));
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) : void {
parent::validateConfigurationForm($form, $form_state);
$block_instance = $this
->getBlockTypeInstance();
if ($block_instance instanceof PluginFormInterface) {
$subform = [
'#parents' => array_merge($form['#parents'], [
'block_config',
]),
];
$block_instance
->validateConfigurationForm($subform, SubformState::createForSubform($subform, $form, $form_state));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) : void {
parent::submitConfigurationForm($form, $form_state);
$block_instance = $this
->getBlockTypeInstance();
if ($block_instance instanceof PluginFormInterface) {
$subform = [
'#parents' => array_merge($form['#parents'], [
'block_config',
]),
];
$block_instance
->submitConfigurationForm($subform, SubformState::createForSubform($subform, $form, $form_state));
$form_state
->setValue([
'block_config',
], $block_instance
->getConfiguration());
}
}
/**
* {@inheritdoc}
*/
public function build(EntityInterface $entity, EntityDisplayInterface $display) : array {
$block = $this
->getBlockTypeInstance();
if (!$block instanceof BlockPluginInterface) {
return [];
}
$element = [];
if ($block instanceof ContextAwarePluginInterface) {
try {
$this
->applyPluginRuntimeContexts($block, [
'display' => EntityContext::fromEntity($display),
'view_mode' => new Context(ContextDefinition::create('string'), $display
->getMode()),
'entity_extra_field.target_entity' => EntityContext::fromEntity($entity),
]);
} catch (\Exception $exception) {
watchdog_exception('entity_extra_field', $exception);
}
}
if ($block
->access($this->currentUser) && ($build = $block
->build())) {
$element = [
'#theme' => 'block',
'#attributes' => [],
'#configuration' => $block
->getConfiguration(),
'#plugin_id' => $block
->getPluginId(),
'#base_plugin_id' => $block
->getBaseId(),
'#derivative_plugin_id' => $block
->getDerivativeId(),
'#id' => str_replace(':', '-', $block
->getPluginId()),
'content' => $build,
];
CacheableMetadata::createFromRenderArray($element)
->merge(CacheableMetadata::createFromRenderArray($element['content']))
->addCacheableDependency($block)
->applyTo($element);
}
return $element;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() : array {
if ($block_type_instance = $this
->getBlockTypeInstance()) {
$this
->calculatePluginDependencies($block_type_instance);
}
return parent::calculateDependencies();
}
/**
* Get block type instance.
*
* @return \Drupal\Core\Block\BlockPluginInterface
* The block instance; otherwise FALSE if type is not defined.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
*/
protected function getBlockTypeInstance() : BlockPluginInterface {
$config = $this
->getConfiguration();
return $this->blockManager
->createInstance($config['block_type'], $config['block_config']);
}
/**
* Get block type options.
*
* @param array $excluded_ids
* An array of block ids to exclude.
*
* @return array
* An array of block type options.
*/
protected function getBlockTypeOptions(array $excluded_ids = []) : array {
$options = [];
// There are a couple block ids that are excluded by default as either
// they're not really needed, or they are causing problems when selected.
$excluded_ids = [
'broken',
'system_branding_block',
] + $excluded_ids;
foreach ($this->blockManager
->getDefinitions() as $block_id => $definition) {
if (!isset($definition['admin_label']) || in_array($block_id, $excluded_ids, TRUE)) {
continue;
}
$category = $definition['category'] ?? $this
->t('Undefined');
$options[(string) $category][$block_id] = $definition['admin_label'];
}
return $options;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencyTrait:: |
protected | property | The object's dependencies. | |
DependencyTrait:: |
protected | function | Adds multiple dependencies. | |
DependencyTrait:: |
protected | function | Adds a dependency. | |
EntityExtraFieldContextTrait:: |
protected | function | Apply the plugin runtime contexts. | |
EntityExtraFieldContextTrait:: |
protected | function | Get the context handler service. | |
EntityExtraFieldContextTrait:: |
protected | function | Get the context repository service. | |
ExtraFieldBlockPlugin:: |
protected | property | ||
ExtraFieldBlockPlugin:: |
private | property | ||
ExtraFieldBlockPlugin:: |
public | function |
Build the render array of the extra field type contents. Overrides ExtraFieldTypePluginInterface:: |
|
ExtraFieldBlockPlugin:: |
public | function |
Form constructor. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldBlockPlugin:: |
public | function |
Calculates dependencies for the configured plugin. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldBlockPlugin:: |
public static | function |
Creates an instance of the plugin. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldBlockPlugin:: |
public | function |
Gets default configuration for this plugin. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldBlockPlugin:: |
protected | function | Get block type instance. | |
ExtraFieldBlockPlugin:: |
protected | function | Get block type options. | |
ExtraFieldBlockPlugin:: |
public | function |
Form submission handler. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldBlockPlugin:: |
public | function |
Form validation handler. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldBlockPlugin:: |
public | function |
Extra field block plugin constructor. Overrides ExtraFieldTypePluginBase:: |
|
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | property | ||
ExtraFieldTypePluginBase:: |
protected | function | Get extra field plugin ajax properties. | |
ExtraFieldTypePluginBase:: |
public | function | Get extra field plugin ajax. | |
ExtraFieldTypePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ExtraFieldTypePluginBase:: |
protected | function | Get entity field reference types. | |
ExtraFieldTypePluginBase:: |
protected | function | Get entity token data. | |
ExtraFieldTypePluginBase:: |
protected | function | Get entity token types. | |
ExtraFieldTypePluginBase:: |
protected | function | Get plugin form state value. | |
ExtraFieldTypePluginBase:: |
protected | function | Get target entity type bundle. | |
ExtraFieldTypePluginBase:: |
protected | function | Get target entity type definition. | |
ExtraFieldTypePluginBase:: |
protected | function | Get target entity type identifier. | |
ExtraFieldTypePluginBase:: |
public | function |
Display the extra field plugin label. Overrides ExtraFieldTypePluginInterface:: |
|
ExtraFieldTypePluginBase:: |
protected | function | Process the entity token text. | |
ExtraFieldTypePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginDependencyTrait:: |
protected | function | Calculates and adds dependencies of a specific plugin instance. | 1 |
PluginDependencyTrait:: |
protected | function | Calculates and returns dependencies of a specific plugin instance. | |
PluginDependencyTrait:: |
protected | function | Wraps the module handler. | 1 |
PluginDependencyTrait:: |
protected | function | Wraps the theme handler. | 1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |