Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains \Drupal\entity\Entity\EntityFormDisplay.
Alex Pott
committed
*/
namespace Drupal\entity\Entity;
Alex Pott
committed
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
Alex Pott
committed
use Drupal\entity\EntityDisplayBase;
/**
* Configuration entity that contains widget options for all components of a
* entity form in a given form mode.
*
Alex Pott
committed
* @ConfigEntityType(
Alex Pott
committed
* id = "entity_form_display",
* label = @Translation("Entity form display"),
* config_prefix = "form_display",
Alex Pott
committed
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "status" = "status"
Alex Pott
committed
* }
* )
*/
class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayInterface {
Alex Pott
committed
/**
* {@inheritdoc}
*/
protected $displayContext = 'form';
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Returns the entity_form_display object used to build an entity form.
*
* Depending on the configuration of the form mode for the entity bundle, this
* can be either the display object associated to the form mode, or the
* 'default' display.
*
* This method should only be used internally when rendering an entity form.
* When assigning suggested display options for a component in a given form
* mode, entity_get_form_display() should be used instead, in order to avoid
* inadvertently modifying the output of other form modes that might happen to
* use the 'default' display too. Those options will then be effectively
* applied only if the form mode is configured to use them.
*
* hook_entity_form_display_alter() is invoked on each display, allowing 3rd
* party code to alter the display options held in the display before they are
* used to generate render arrays.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity for which the form is being built.
* @param string $form_mode
* The form mode.
*
* @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface
* The display object that should be used to build the entity form.
*
* @see entity_get_form_display()
* @see hook_entity_form_display_alter()
*/
public static function collectRenderDisplay($entity, $form_mode) {
$entity_type = $entity->getEntityTypeId();
$bundle = $entity->bundle();
// Check the existence and status of:
// - the display for the form mode,
// - the 'default' display.
if ($form_mode != 'default') {
$candidate_ids[] = $entity_type . '.' . $bundle . '.' . $form_mode;
}
$candidate_ids[] = $entity_type . '.' . $bundle . '.default';
$results = \Drupal::entityQuery('entity_form_display')
->condition('id', $candidate_ids)
->condition('status', TRUE)
->execute();
// Load the first valid candidate display, if any.
$storage = \Drupal::entityManager()->getStorageController('entity_form_display');
foreach ($candidate_ids as $candidate_id) {
if (isset($results[$candidate_id])) {
$display = $storage->load($candidate_id);
break;
}
}
// Else create a fresh runtime object.
if (empty($display)) {
$display = $storage->create(array(
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => $form_mode,
'status' => TRUE,
));
}
// Let the display know which form mode was originally requested.
$display->originalMode = $form_mode;
// Let modules alter the display.
$display_context = array(
'entity_type' => $entity_type,
'bundle' => $bundle,
'form_mode' => $form_mode,
);
\Drupal::moduleHandler()->alter('entity_form_display', $display, $display_context);
return $display;
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type) {
$this->pluginManager = \Drupal::service('plugin.manager.field.widget');
parent::__construct($values, $entity_type);
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getRenderer($field_name) {
Alex Pott
committed
if (isset($this->plugins[$field_name])) {
return $this->plugins[$field_name];
}
// Instantiate the widget object from the stored display properties.
if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) {
Alex Pott
committed
$widget = $this->pluginManager->getInstance(array(
'field_definition' => $definition,
Alex Pott
committed
'form_mode' => $this->originalMode,
// No need to prepare, defaults have been merged in setComponent().
'prepare' => FALSE,
'configuration' => $configuration
));
}
else {
$widget = NULL;
}
// Persist the widget object.
$this->plugins[$field_name] = $widget;
return $widget;
}
/**
* {@inheritdoc}
*/
public function __sleep() {
Alex Pott
committed
// Only store the definition, not external objects or derived data.
$keys = array_keys($this->getExportProperties());
$keys[] = 'entityTypeId';
return $keys;
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public function __wakeup() {
// Run the values from getExportProperties() through __construct().
$values = array_intersect_key($this->getExportProperties(), get_object_vars($this));
$this->__construct($values, $this->entityTypeId);