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\Annotation\EntityType;
use Drupal\Core\Annotation\Translation;
use Drupal\entity\EntityDisplayBase;
use Drupal\entity\EntityFormDisplayInterface;
/**
* Configuration entity that contains widget options for all components of a
* entity form in a given form mode.
*
* @EntityType(
* id = "entity_form_display",
* label = @Translation("Entity form display"),
* module = "entity",
* controllers = {
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
* },
* config_prefix = "entity.form_display",
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "status" = "status"
Alex Pott
committed
* }
* )
*/
class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayInterface, \Serializable {
/**
* {@inheritdoc}
*/
public function __construct(array $values, $entity_type) {
$this->pluginManager = \Drupal::service('plugin.manager.field.widget');
$this->displayContext = 'form';
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)) {
$instance = field_info_instance($this->targetEntityType, $field_name, $this->bundle);
$widget = $this->pluginManager->getInstance(array(
'field_definition' => $instance,
Alex Pott
committed
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
'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 serialize() {
// Only store the definition, not external objects or derived data.
$data = $this->getExportProperties() + array('entityType' => $this->entityType());
return serialize($data);
}
/**
* {@inheritdoc}
*/
public function unserialize($serialized) {
$data = unserialize($serialized);
$entity_type = $data['entityType'];
unset($data['entityType']);
$this->__construct($data, $entity_type);
}
}