3 namespace Drupal\field_layout;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
9 use Drupal\Core\Layout\LayoutPluginManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Builds a field layout.
15 class FieldLayoutBuilder implements ContainerInjectionInterface {
18 * The layout plugin manager.
20 * @var \Drupal\Core\Layout\LayoutPluginManagerInterface
22 protected $layoutPluginManager;
25 * The entity field manager.
27 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
29 protected $entityFieldManager;
32 * Constructs a new FieldLayoutBuilder.
34 * @param \Drupal\Core\Layout\LayoutPluginManagerInterface $layout_plugin_manager
35 * The layout plugin manager.
36 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
37 * The entity field manager.
39 public function __construct(LayoutPluginManagerInterface $layout_plugin_manager, EntityFieldManagerInterface $entity_field_manager) {
40 $this->layoutPluginManager = $layout_plugin_manager;
41 $this->entityFieldManager = $entity_field_manager;
47 public static function create(ContainerInterface $container) {
49 $container->get('plugin.manager.core.layout'),
50 $container->get('entity_field.manager')
55 * Applies the layout to an entity build.
58 * A renderable array representing the entity content or form.
59 * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
60 * The entity display holding the display options configured for the entity
63 public function buildView(array &$build, EntityDisplayWithLayoutInterface $display) {
64 $layout_definition = $this->layoutPluginManager->getDefinition($display->getLayoutId(), FALSE);
65 if ($layout_definition && $fields = $this->getFields($build, $display, 'view')) {
66 // Add the regions to the $build in the correct order.
67 $regions = array_fill_keys($layout_definition->getRegionNames(), []);
69 foreach ($fields as $name => $field) {
70 // Move the field from the top-level of $build into a region-specific
72 // @todo Ideally the array structure would remain unchanged, see
73 // https://www.drupal.org/node/2846393.
74 $regions[$field['region']][$name] = $build[$name];
77 // Ensure this will not conflict with any existing array elements by
78 // prefixing with an underscore.
79 $build['_field_layout'] = $display->getLayout()->build($regions);
84 * Applies the layout to an entity form.
87 * A renderable array representing the entity content or form.
88 * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
89 * The entity display holding the display options configured for the entity
92 public function buildForm(array &$build, EntityDisplayWithLayoutInterface $display) {
93 $layout_definition = $this->layoutPluginManager->getDefinition($display->getLayoutId(), FALSE);
94 if ($layout_definition && $fields = $this->getFields($build, $display, 'form')) {
96 $fill['#process'][] = '\Drupal\Core\Render\Element\RenderElement::processGroup';
97 $fill['#pre_render'][] = '\Drupal\Core\Render\Element\RenderElement::preRenderGroup';
98 // Add the regions to the $build in the correct order.
99 $regions = array_fill_keys($layout_definition->getRegionNames(), $fill);
101 foreach ($fields as $name => $field) {
102 // As this is a form, #group can be used to relocate the fields. This
103 // avoids breaking hook_form_alter() implementations by not actually
104 // moving the field in the form structure. If a #group is already set,
105 // do not overwrite it.
106 if (!isset($build[$name]['#group'])) {
107 $build[$name]['#group'] = $field['region'];
110 // Ensure this will not conflict with any existing array elements by
111 // prefixing with an underscore.
112 $build['_field_layout'] = $display->getLayout()->build($regions);
117 * Gets the fields that need to be processed.
119 * @param array $build
120 * A renderable array representing the entity content or form.
121 * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
122 * The entity display holding the display options configured for the entity
124 * @param string $display_context
125 * The display context, either 'form' or 'view'.
128 * An array of configurable fields present in the build.
130 protected function getFields(array $build, EntityDisplayWithLayoutInterface $display, $display_context) {
131 $components = $display->getComponents();
133 // Ignore any extra fields from the list of field definitions. Field
134 // definitions can have a non-configurable display, but all extra fields are
136 $field_definitions = array_diff_key(
137 $this->entityFieldManager->getFieldDefinitions($display->getTargetEntityTypeId(), $display->getTargetBundle()),
138 $this->entityFieldManager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle())
141 $fields_to_exclude = array_filter($field_definitions, function (FieldDefinitionInterface $field_definition) use ($display_context) {
142 // Remove fields with a non-configurable display.
143 return !$field_definition->isDisplayConfigurable($display_context);
145 $components = array_diff_key($components, $fields_to_exclude);
147 // Only include fields present in the build.
148 $components = array_intersect_key($components, $build);