Skip to content
Snippets Groups Projects

Issue #3362625: CustomElementFormatter plugin type for entity_ce_display

Compare and
14 files
+ 656
153
Compare changes
  • Side-by-side
  • Inline
Files
14
@@ -2,12 +2,18 @@
namespace Drupal\custom_elements_ui\Form;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\Field\PluginSettingsInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\custom_elements\CustomElementsFieldFormatterPluginManager;
use Drupal\field_ui\FieldUI;
use Drupal\field_ui\Form\EntityDisplayFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -20,6 +26,38 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
/**
* The custom element formatter plugin manager.
*
* @var \Drupal\custom_elements\CustomElementsFieldFormatterPluginManager
*/
protected $customElementFormatterPluginManager;
/**
* Constructs a new EntityCustomElementsDisplayEditForm.
*
* @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
* The field type manager.
* @param \Drupal\Component\Plugin\PluginManagerBase $plugin_manager
* The widget or formatter plugin manager.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* (optional) The entity display_repository.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
* (optional) The entity field manager.
* @param \Drupal\custom_elements\CustomElementsFieldFormatterPluginManager $ce_formatter_plugin_manager
* The custom element formatter plugin manager.
*/
public function __construct(
FieldTypePluginManagerInterface $field_type_manager,
PluginManagerBase $plugin_manager,
EntityDisplayRepositoryInterface $entity_display_repository,
EntityFieldManagerInterface $entity_field_manager,
CustomElementsFieldFormatterPluginManager $ce_formatter_plugin_manager
) {
parent::__construct($field_type_manager, $plugin_manager, $entity_display_repository, $entity_field_manager);
$this->customElementFormatterPluginManager = $ce_formatter_plugin_manager;
}
/**
* {@inheritdoc}
*/
@@ -29,7 +67,8 @@ class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
$container->get('plugin.manager.field.field_type'),
$container->get('plugin.manager.field.formatter'),
$container->get('entity_display.repository'),
$container->get('entity_field.manager')
$container->get('entity_field.manager'),
$container->get('custom_elements.plugin.manager.field.custom_element_formatter')
);
}
@@ -80,7 +119,6 @@ class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
];
// And add is_slot.
// @todo Consider making it as separate region.
$ce_row_fields['is_slot'] = [
'#type' => 'checkbox',
'#title' => $this->t('Is Slot'),
@@ -88,6 +126,15 @@ class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
'#default_value' => $display_options['is_slot'] ?? FALSE,
];
$ce_formatter_default = $display_options['ce_formatter'] ?? FALSE;
$ce_row_fields['ce_formatter'] = [
'#type' => 'select',
'#title' => $this->t('Custom Element Formatter'),
'#options' => $this->getApplicableCeFormatterOptions($field_definition),
'#title_display' => 'invisible',
'#default_value' => $ce_formatter_default,
];
// Add needed fields on specific position.
$ce_position = array_search('plugin', array_keys($field_row));
$field_row = array_slice($field_row, 0, $ce_position, TRUE) + $ce_row_fields + array_slice($field_row, $ce_position, NULL, TRUE);
@@ -107,6 +154,7 @@ class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
$component_options = $entity->getComponent($field_name);
$values = $form_values['fields'][$field_name];
if ($values['region'] !== 'hidden') {
$component_options['ce_formatter'] = $values['ce_formatter'];
$component_options['name'] = $values['name'];
$component_options['is_slot'] = (bool) $values['is_slot'];
$entity->setComponent($field_name, $component_options);
@@ -127,6 +175,7 @@ class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
$this->t('Region'),
$this->t('Name'),
$this->t('Is Slot'),
$this->t('Custom Element Formatter'),
['data' => $this->t('Formatter'), 'colspan' => 3],
];
}
@@ -247,21 +296,24 @@ class EntityCustomElementsDisplayEditForm extends EntityDisplayFormBase {
}
/**
* Returns an array of applicable widget or formatter options for a field.
* Returns an array of applicable CE formatter options for a field.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
*
* @return array
* An array of applicable widget or formatter options.
* An array of applicable CE formatter options.
*/
protected function getApplicablePluginOptions(FieldDefinitionInterface $field_definition) {
$applicable_options = parent::getApplicablePluginOptions($field_definition);
// @todo Discuss final implementation for plugins.
return array_merge([
'raw' => $this->t('Raw'),
'auto' => $this->t('Auto'),
], $applicable_options);
protected function getApplicableCeFormatterOptions(FieldDefinitionInterface $field_definition) {
$options = $this->customElementFormatterPluginManager->getOptions($field_definition->getType());
$applicable_options = [];
foreach ($options as $option => $label) {
$plugin_class = DefaultFactory::getPluginClass($option, $this->customElementFormatterPluginManager->getDefinition($option));
if ($plugin_class::isApplicable($field_definition)) {
$applicable_options[$option] = $label;
}
}
return $applicable_options;
}
}
Loading