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 // If the region is controlled by the layout, move the field from the
71 // top-level of $build into a region-specific section. Custom regions
72 // could be set by other code at run-time; these should be ignored.
73 // @todo Ideally the array structure would remain unchanged, see
74 // https://www.drupal.org/node/2846393.
75 if (isset($regions[$field['region']])) {
76 $regions[$field['region']][$name] = $build[$name];
80 // Ensure this will not conflict with any existing array elements by
81 // prefixing with an underscore.
82 $build['_field_layout'] = $display->getLayout()->build($regions);
87 * Applies the layout to an entity form.
90 * A renderable array representing the entity content or form.
91 * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
92 * The entity display holding the display options configured for the entity
95 public function buildForm(array &$build, EntityDisplayWithLayoutInterface $display) {
96 $layout_definition = $this->layoutPluginManager->getDefinition($display->getLayoutId(), FALSE);
97 if ($layout_definition && $fields = $this->getFields($build, $display, 'form')) {
99 $fill['#process'][] = '\Drupal\Core\Render\Element\RenderElement::processGroup';
100 $fill['#pre_render'][] = '\Drupal\Core\Render\Element\RenderElement::preRenderGroup';
101 // Add the regions to the $build in the correct order.
102 $regions = array_fill_keys($layout_definition->getRegionNames(), $fill);
104 foreach ($fields as $name => $field) {
105 // As this is a form, #group can be used to relocate the fields. This
106 // avoids breaking hook_form_alter() implementations by not actually
107 // moving the field in the form structure. If a #group is already set,
108 // do not overwrite it.
109 if (isset($regions[$field['region']]) && !isset($build[$name]['#group'])) {
110 $build[$name]['#group'] = $field['region'];
113 // Ensure this will not conflict with any existing array elements by
114 // prefixing with an underscore.
115 $build['_field_layout'] = $display->getLayout()->build($regions);
120 * Gets the fields that need to be processed.
122 * @param array $build
123 * A renderable array representing the entity content or form.
124 * @param \Drupal\field_layout\Display\EntityDisplayWithLayoutInterface $display
125 * The entity display holding the display options configured for the entity
127 * @param string $display_context
128 * The display context, either 'form' or 'view'.
131 * An array of configurable fields present in the build.
133 protected function getFields(array $build, EntityDisplayWithLayoutInterface $display, $display_context) {
134 $components = $display->getComponents();
136 // Ignore any extra fields from the list of field definitions. Field
137 // definitions can have a non-configurable display, but all extra fields are
139 $field_definitions = array_diff_key(
140 $this->entityFieldManager->getFieldDefinitions($display->getTargetEntityTypeId(), $display->getTargetBundle()),
141 $this->entityFieldManager->getExtraFields($display->getTargetEntityTypeId(), $display->getTargetBundle())
144 $fields_to_exclude = array_filter($field_definitions, function (FieldDefinitionInterface $field_definition) use ($display_context) {
145 // Remove fields with a non-configurable display.
146 return !$field_definition->isDisplayConfigurable($display_context);
148 $components = array_diff_key($components, $fields_to_exclude);
150 // Only include fields present in the build.
151 $components = array_intersect_key($components, $build);