Loading modules/custom_elements_ui/custom_elements_ui.module +1 −0 Original line number Diff line number Diff line Loading @@ -81,5 +81,6 @@ function _custom_elements_ui_entity_view_display_edit_form_submit($form, FormSta /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display_entity */ $display_entity = $display_form->getEntity(); $display_entity->setThirdPartySetting('custom_elements_ui', 'enabled', $set_enabled); // @todo Autogenerate fields and name from view display entity? $display_entity->save(); } modules/custom_elements_ui/custom_elements_ui.services.yml +4 −0 Original line number Diff line number Diff line Loading @@ -4,3 +4,7 @@ services: arguments: [ '@entity_type.manager' ] tags: - { name: event_subscriber } custom_elements_ui.processor.default_content_entity_ui: class: Drupal\custom_elements_ui\Processor\DefaultContentEntityUiProcessor tags: - { name: custom_elements_processor, priority: 100 } modules/custom_elements_ui/src/Form/EntityCustomElementsDisplayForm.php +4 −2 Original line number Diff line number Diff line Loading @@ -136,16 +136,18 @@ class EntityCustomElementsDisplayForm extends EntityDisplayFormBase { } $options['type'] = $values['type']; $options['weight'] = $values['weight']; $options['weight'] = (int) $values['weight']; $options['region'] = $values['region']; // Only formatters have configurable label visibility. if (isset($values['label'])) { $options['label'] = $values['label']; } $options['is_slot'] = $values['is_slot']; $options['is_slot'] = (bool) $values['is_slot']; $ce_fields[$field_name] = $options; } } uasort($ce_fields, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); $entity->setThirdPartySetting('custom_elements_ui', 'custom_element_fields', $ce_fields); $entity->setThirdPartySetting('custom_elements_ui', 'custom_element_name', $form_values['ce_name']); } Loading modules/custom_elements_ui/src/Processor/DefaultContentEntityUiProcessor.php 0 → 100644 +71 −0 Original line number Diff line number Diff line <?php namespace Drupal\custom_elements_ui\Processor; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\custom_elements\CustomElement; use Drupal\custom_elements\CustomElementGeneratorTrait; use Drupal\custom_elements\CustomElementsProcessorFieldUtilsTrait; use Drupal\custom_elements\Processor\CustomElementProcessorInterface; /** * Custom elements ui processor for content entities. */ class DefaultContentEntityUiProcessor implements CustomElementProcessorInterface { use CustomElementGeneratorTrait; use CustomElementsProcessorFieldUtilsTrait; /** * {@inheritdoc} */ public function supports($data, $view_mode) { if ($data instanceof ContentEntityInterface) { $display = EntityViewDisplay::collectRenderDisplay($data, $view_mode); $is_enabled = $display->getThirdPartySetting('custom_elements_ui', 'enabled', FALSE); // @todo Do not check it here but have proper validation instead. $ce_name = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_name', ''); $ce_fields = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_fields', []); $is_configured_properly = !empty($ce_name) && !empty($ce_fields); return $is_enabled && $is_configured_properly; } return FALSE; } /** * {@inheritdoc} */ public function addtoElement($data, CustomElement $element, $view_mode) { assert($data instanceof ContentEntityInterface); $entity = $data; $display = EntityViewDisplay::collectRenderDisplay($data, $view_mode); $ce_fields = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_fields', []); $ce_name = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_name', ''); $element->setTag($ce_name); foreach ($ce_fields as $field_name => $options) { if ($this->fieldIsAccessible($entity, $field_name, $element)) { if ($options['is_slot']) { $nested_elements = []; foreach ($entity->get($field_name) as $field_item) { $nested_element = new CustomElement(); $nested_element->setTagPrefix('field'); $nested_element->setTag($entity->get($field_name)->getFieldDefinition()->getType()); $this->getCustomElementGenerator()->process($field_item, $nested_element, $view_mode); $nested_elements[] = $nested_element; } $element->setSlotFromNestedElements($options['label'], $nested_elements); } else { $field_items = $entity->get($field_name); $property = $field_items->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName(); $element->setAttribute($options['label'], $field_items->{$property}); } } } } } Loading
modules/custom_elements_ui/custom_elements_ui.module +1 −0 Original line number Diff line number Diff line Loading @@ -81,5 +81,6 @@ function _custom_elements_ui_entity_view_display_edit_form_submit($form, FormSta /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display_entity */ $display_entity = $display_form->getEntity(); $display_entity->setThirdPartySetting('custom_elements_ui', 'enabled', $set_enabled); // @todo Autogenerate fields and name from view display entity? $display_entity->save(); }
modules/custom_elements_ui/custom_elements_ui.services.yml +4 −0 Original line number Diff line number Diff line Loading @@ -4,3 +4,7 @@ services: arguments: [ '@entity_type.manager' ] tags: - { name: event_subscriber } custom_elements_ui.processor.default_content_entity_ui: class: Drupal\custom_elements_ui\Processor\DefaultContentEntityUiProcessor tags: - { name: custom_elements_processor, priority: 100 }
modules/custom_elements_ui/src/Form/EntityCustomElementsDisplayForm.php +4 −2 Original line number Diff line number Diff line Loading @@ -136,16 +136,18 @@ class EntityCustomElementsDisplayForm extends EntityDisplayFormBase { } $options['type'] = $values['type']; $options['weight'] = $values['weight']; $options['weight'] = (int) $values['weight']; $options['region'] = $values['region']; // Only formatters have configurable label visibility. if (isset($values['label'])) { $options['label'] = $values['label']; } $options['is_slot'] = $values['is_slot']; $options['is_slot'] = (bool) $values['is_slot']; $ce_fields[$field_name] = $options; } } uasort($ce_fields, 'Drupal\Component\Utility\SortArray::sortByWeightElement'); $entity->setThirdPartySetting('custom_elements_ui', 'custom_element_fields', $ce_fields); $entity->setThirdPartySetting('custom_elements_ui', 'custom_element_name', $form_values['ce_name']); } Loading
modules/custom_elements_ui/src/Processor/DefaultContentEntityUiProcessor.php 0 → 100644 +71 −0 Original line number Diff line number Diff line <?php namespace Drupal\custom_elements_ui\Processor; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\custom_elements\CustomElement; use Drupal\custom_elements\CustomElementGeneratorTrait; use Drupal\custom_elements\CustomElementsProcessorFieldUtilsTrait; use Drupal\custom_elements\Processor\CustomElementProcessorInterface; /** * Custom elements ui processor for content entities. */ class DefaultContentEntityUiProcessor implements CustomElementProcessorInterface { use CustomElementGeneratorTrait; use CustomElementsProcessorFieldUtilsTrait; /** * {@inheritdoc} */ public function supports($data, $view_mode) { if ($data instanceof ContentEntityInterface) { $display = EntityViewDisplay::collectRenderDisplay($data, $view_mode); $is_enabled = $display->getThirdPartySetting('custom_elements_ui', 'enabled', FALSE); // @todo Do not check it here but have proper validation instead. $ce_name = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_name', ''); $ce_fields = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_fields', []); $is_configured_properly = !empty($ce_name) && !empty($ce_fields); return $is_enabled && $is_configured_properly; } return FALSE; } /** * {@inheritdoc} */ public function addtoElement($data, CustomElement $element, $view_mode) { assert($data instanceof ContentEntityInterface); $entity = $data; $display = EntityViewDisplay::collectRenderDisplay($data, $view_mode); $ce_fields = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_fields', []); $ce_name = $display->getThirdPartySetting('custom_elements_ui', 'custom_element_name', ''); $element->setTag($ce_name); foreach ($ce_fields as $field_name => $options) { if ($this->fieldIsAccessible($entity, $field_name, $element)) { if ($options['is_slot']) { $nested_elements = []; foreach ($entity->get($field_name) as $field_item) { $nested_element = new CustomElement(); $nested_element->setTagPrefix('field'); $nested_element->setTag($entity->get($field_name)->getFieldDefinition()->getType()); $this->getCustomElementGenerator()->process($field_item, $nested_element, $view_mode); $nested_elements[] = $nested_element; } $element->setSlotFromNestedElements($options['label'], $nested_elements); } else { $field_items = $entity->get($field_name); $property = $field_items->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName(); $element->setAttribute($options['label'], $field_items->{$property}); } } } } }