Skip to content
Snippets Groups Projects
Commit cd213674 authored by Wolfgang Ziegler's avatar Wolfgang Ziegler
Browse files

Issue #3445143 by fago: Separate configuration of CE field formatters and display components.

parent 9a20fa80
No related branches found
No related tags found
1 merge request!50Separate config
Pipeline #166299 passed
......@@ -34,10 +34,7 @@ custom_elements.entity_ce_display.*.*.*:
mapping:
formatter:
type: string
label: 'Custom element formatter type machine name'
label:
type: string
label: 'Keep it only for compatibility with FieldFormatters'
label: 'Custom element formatter ID'
name:
type: string
label: 'Custom element attribute/slot name'
......@@ -50,18 +47,33 @@ custom_elements.entity_ce_display.*.*.*:
region:
type: string
label: 'Region'
# @todo Must be checked/corrected later.
settings:
type: field.formatter.settings.[%parent.type]
configuration:
type: custom_elements.field_formatter.configuration.[%parent.formatter]
label: 'Settings'
third_party_settings:
type: sequence
label: 'Third party settings'
sequence:
type: field.formatter.third_party.[%key]
hidden:
type: sequence
label: 'Hidden'
sequence:
type: boolean
label: 'Value'
# Default schema for entity display field with undefined type.
custom_elements.field_formatter.configuration.*:
type: mapping
# Schema for core field formatter plugin.
custom_elements.field_formatter.configuration.field:*:
type: mapping
label: 'Field formatter settings'
mapping:
type:
type: string
label: 'Formatter type machine name'
settings:
type: field.formatter.settings.[%parent.type]
label: 'Settings'
third_party_settings:
type: sequence
label: 'Third party settings'
sequence:
type: field.formatter.third_party.[%key]
......@@ -140,21 +140,21 @@ abstract class CustomElementsFieldFormatterBase extends PluginBase implements Cu
* {@inheritdoc}
*/
public function getSettings() {
return $this->getConfiguration();
return $this->configuration['settings'] ?? [];
}
/**
* {@inheritdoc}
*/
public function getSetting($key) {
return $$this->configuration[$key] ?? NULL;
return $this->configuration['settings'][$key] ?? NULL;
}
/**
* {@inheritdoc}
*/
public function setSettings(array $settings) {
$this->configuration = $settings;
$this->configuration['settings'] = $settings;
return $this;
}
......@@ -162,7 +162,7 @@ abstract class CustomElementsFieldFormatterBase extends PluginBase implements Cu
* {@inheritdoc}
*/
public function setSetting($key, $value) {
$this->configuration[$key] = $value;
$this->configuration['settings'][$key] = $value;
return $this;
}
......
......@@ -6,8 +6,8 @@ use Drupal\Core\Entity\EntityDisplayBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Plugin\DefaultLazyPluginCollection as DefaultLazyPluginCollectionAlias;
use Drupal\custom_elements\CustomElementGenerator;
use Drupal\custom_elements\LazyFormatterPluginCollection;
/**
* Custom element display configuration entity.
......@@ -121,17 +121,18 @@ class EntityCeDisplay extends EntityDisplayBase implements EntityCeDisplayInterf
* {@inheritdoc}
*/
public function getRenderer($field_name) {
// @todo Implement PluginSettingsInterface in
// CustomElementsFieldFormatterInterface.
if (isset($this->plugins[$field_name])) {
return $this->plugins[$field_name];
}
// Instantiate the formatter object from the stored display properties.
if (($configuration = $this->getComponent($field_name)) && isset($configuration['formatter']) && ($definition = $this->getFieldDefinition($field_name))) {
$formatter = $this->pluginManager->createInstance($configuration['formatter'], [
if (($component = $this->getComponent($field_name)) && isset($component['formatter']) && ($definition = $this->getFieldDefinition($field_name))) {
$component += ['configuration' => [], 'name' => $field_name, 'is_slot' => FALSE];
$formatter = $this->pluginManager->createInstance($component['formatter'], [
'field_definition' => $definition,
'view_mode' => $this->originalMode,
] + $configuration
'name' => $component['name'],
'is_slot' => $component['is_slot'],
] + $component['configuration']
);
}
else {
......@@ -148,16 +149,20 @@ class EntityCeDisplay extends EntityDisplayBase implements EntityCeDisplayInterf
*/
public function getPluginCollections() {
$configurations = [];
foreach ($this->getComponents() as $field_name => $configuration) {
if (!empty($configuration['formatter']) && ($field_definition = $this->getFieldDefinition($field_name))) {
$configurations[$field_name] = $configuration + [
foreach ($this->getComponents() as $field_name => $component) {
if (!empty($component['formatter']) && ($field_definition = $this->getFieldDefinition($field_name))) {
$component += ['configuration' => [], 'name' => $field_name, 'is_slot' => FALSE];
$configurations[$field_name] = [
'id' => $component['formatter'],
'field_definition' => $field_definition,
'view_mode' => $this->originalMode,
];
'name' => $component['name'],
'is_slot' => $component['is_slot'],
] + $component['configuration'];
}
}
return [
'formatters' => new LazyFormatterPluginCollection($this->pluginManager, $configurations),
'formatters' => new DefaultLazyPluginCollectionAlias($this->pluginManager, $configurations),
];
}
......@@ -165,9 +170,8 @@ class EntityCeDisplay extends EntityDisplayBase implements EntityCeDisplayInterf
* {@inheritdoc}
*/
protected function init() {
$test = $this->getComponent('title');
return $test;
// Skip this.
// @todo Should this be used instead of ::postCreate()?
}
/**
......
<?php
namespace Drupal\custom_elements;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
/**
* A collection of formatters or widgets.
*/
class LazyFormatterPluginCollection extends DefaultLazyPluginCollection {
/**
* The key within the plugin configuration that contains the plugin ID.
*
* @var string
*/
protected $pluginKey = 'formatter';
}
......@@ -96,7 +96,12 @@ class CoreFieldCeFieldFormatter extends CustomElementsFieldFormatterBase {
return $this->formatterManager->getInstance([
'field_definition' => $this->getFieldDefinition(),
'view_mode' => $this->getViewMode(),
'configuration' => ['type' => $formatter_id] + $this->configuration,
'configuration' => [
'type' => $formatter_id,
'label' => 'hidden',
'settings' => $this->configuration['settings'] ?? [],
'third_party_settings' => $this->configuration['third_party_settings'] ?? [],
],
]);
}
......
......@@ -2,6 +2,8 @@
namespace Drupal\Tests\custom_elements\Kernel;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\custom_elements\CustomElementsFieldFormatterInterface;
use Drupal\custom_elements\Entity\EntityCeDisplay;
use Drupal\KernelTests\KernelTestBase;
......@@ -12,6 +14,8 @@ use Drupal\KernelTests\KernelTestBase;
*/
class EntityCeDisplayTest extends KernelTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*
......@@ -46,8 +50,12 @@ class EntityCeDisplayTest extends KernelTestBase {
// Check that arbitrary options are correctly stored.
$expected['component_1'] = [
'weight' => 10,
'third_party_settings' => ['field_test' => ['foo' => 'bar']],
'settings' => [],
'name' => 'component',
'region' => 'content',
'configuration' => [
'third_party_settings' => ['field_test' => ['foo' => 'bar']],
'settings' => [],
],
];
$ce_display->setComponent('component_1', $expected['component_1']);
$this->assertEquals($expected['component_1'], $ce_display->getComponent('component_1'));
......@@ -67,4 +75,36 @@ class EntityCeDisplayTest extends KernelTestBase {
$this->assertNull(EntityCeDisplay::load($ce_display_id), 'The entity was deleted successfully.');
}
/**
* Tests config of core field formatters is correctly used.
*/
public function testCoreFieldFormatterComponents() {
$ce_display = EntityCeDisplay::create([
'targetEntityType' => 'entity_test',
'customElementName' => 'name_test',
'bundle' => 'entity_test',
'mode' => 'default',
]);
$ce_display->setComponent('user_id', [
'name' => 'user',
'is_slot' => TRUE,
'formatter' => 'field:entity_reference_entity_view',
'configuration' => [
'settings' => ['view_mode' => 'some_teaser'],
],
]);
// Save and load to make sure config stays.
$ce_display->save();
$ce_display = EntityCeDisplay::load($ce_display->id());
$formatter = $ce_display->getRenderer('user_id');
$this->assertEquals('user', $formatter->getConfiguration()['name']);
$this->assertEquals(TRUE, $formatter->getConfiguration()['is_slot']);
$this->assertInstanceOf(CustomElementsFieldFormatterInterface::class, $formatter);
$this->assertEquals(
[$this->t('Rendered as @mode', ['@mode' => 'some_teaser'])],
$formatter->settingsSummary()
);
$this->assertEquals('some_teaser', $formatter->getSetting('view_mode'));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment