EntityFormDisplay.php 2.36 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity\Entity\EntityFormDisplay.
6 7
 */

8
namespace Drupal\entity\Entity;
9

10
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use Drupal\entity\EntityDisplayBase;

/**
 * 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"),
 *   controllers = {
 *     "storage" = "Drupal\Core\Config\Entity\ConfigStorageController"
 *   },
 *   config_prefix = "entity.form_display",
 *   entity_keys = {
 *     "id" = "id",
26 27
 *     "uuid" = "uuid",
 *     "status" = "status"
28 29 30 31 32
 *   }
 * )
 */
class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayInterface, \Serializable {

33 34 35 36 37
  /**
   * {@inheritdoc}
   */
  protected $displayContext = 'form';

38 39 40 41 42 43 44 45 46 47 48 49
  /**
   * {@inheritdoc}
   */
  public function __construct(array $values, $entity_type) {
    $this->pluginManager = \Drupal::service('plugin.manager.field.widget');

    parent::__construct($values, $entity_type);
  }

  /**
   * {@inheritdoc}
   */
50
  public function getRenderer($field_name) {
51 52 53 54 55
    if (isset($this->plugins[$field_name])) {
      return $this->plugins[$field_name];
    }

    // Instantiate the widget object from the stored display properties.
56
    if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) {
57
      $widget = $this->pluginManager->getInstance(array(
58
        'field_definition' => $definition,
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);
  }

}