public function ComponentBlock::formatterSettingsProcessCallback in Component blocks 1.0.x
Same name and namespace in other branches
- 1.x src/Plugin/Block/ComponentBlock.php \Drupal\component_blocks\Plugin\Block\ComponentBlock::formatterSettingsProcessCallback()
- 1.1.x src/Plugin/Block/ComponentBlock.php \Drupal\component_blocks\Plugin\Block\ComponentBlock::formatterSettingsProcessCallback()
Render API callback: builds the formatter settings elements.
File
- src/
Plugin/ Block/ ComponentBlock.php, line 300
Class
- ComponentBlock
- Defines a class for a specially shaped block.
Namespace
Drupal\component_blocks\Plugin\BlockCode
public function formatterSettingsProcessCallback(array &$element, FormStateInterface $form_state, array &$complete_form) {
if ($configuration = $this
->getCurrentConfiguration($element['#parents'], $form_state)) {
if ($configuration['source'] === self::FIXED) {
$element['value'] = [
'#type' => 'textfield',
'#default_value' => $configuration['value'] ?? '',
'#title' => $this
->t('Fixed value'),
];
return $element;
}
$contexts = $this
->contextHandler()
->getMatchingContexts($form_state
->getTemporaryValue('gathered_contexts') ?: [], $this
->getContextDefinition('entity'));
// Contexts can become empty on subsequent ajax requests with layout
// builder.
if (!$contexts) {
$contexts = $this
->contextHandler()
->getMatchingContexts($this
->getAvailableContexts($form_state
->getBuildInfo()['args'][0]), $this
->getContextDefinition('entity'));
}
$context = reset($contexts);
/** @var \Drupal\Core\Entity\ContentEntityInterface $sample_entity */
$sample_entity = $context
->getContextData()
->getValue();
$field_definition = $sample_entity
->getFieldDefinition($configuration['source']);
$formatter_configuration = array_intersect_key($configuration, [
'type' => TRUE,
'settings' => TRUE,
]) + [
'label' => 'hidden',
];
$options = $this
->getApplicablePluginOptions($field_definition);
$keys = array_keys($options);
$formatter_configuration += [
'type' => reset($keys),
'settings' => $this
->formatterPluginManager()
->getDefaultSettings(reset($keys)),
];
$formatter = $this
->formatterPluginManager()
->getInstance([
'configuration' => $formatter_configuration,
'field_definition' => $field_definition,
'view_mode' => EntityDisplayBase::CUSTOM_MODE,
'prepare' => TRUE,
]);
$element['source']['#default_value'] = $configuration['source'];
$element['type'] = [
'#type' => 'select',
'#options' => $options,
'#default_value' => $formatter_configuration['type'],
'#required' => TRUE,
'#title' => $this
->t('Formatter'),
'#ajax' => [
'callback' => [
static::class,
'updateElementValue',
],
'wrapper' => 'component-settings-' . end($element['#parents']),
],
];
$element['settings'] = $formatter
->settingsForm($complete_form, $form_state);
$element['settings']['#parents'] = array_merge($element['#parents'], [
'settings',
]);
}
return $element;
}