ContentEntityForm.php 4.07 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\ContentEntityForm.
6 7 8 9
 */

namespace Drupal\Core\Entity;

10
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
11
use Drupal\Core\Entity\Entity\EntityFormDisplay;
12
use Drupal\Core\Form\FormStateInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14 15

/**
16
 * Entity form variant for content entity types.
17 18 19
 *
 * @see \Drupal\Core\ContentEntityBase
 */
20
class ContentEntityForm extends EntityForm implements ContentEntityFormInterface {
21

22 23 24 25 26 27 28 29
  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
30
   * Constructs a ContentEntityForm object.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   */
  public function __construct(EntityManagerInterface $entity_manager) {
    $this->entityManager = $entity_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.manager')
    );
  }

48 49 50
  /**
   * {@inheritdoc}
   */
51
  public function form(array $form, FormStateInterface $form_state) {
52 53
    $form = parent::form($form, $form_state);
    $this->getFormDisplay($form_state)->buildForm($this->entity, $form, $form_state);
54 55 56 57 58 59
    return $form;
  }

  /**
   * {@inheritdoc}
   */
60
  public function validate(array $form, FormStateInterface $form_state) {
61
    $this->updateFormLangcode($form_state);
62
    $entity = $this->buildEntity($form, $form_state);
63
    $this->getFormDisplay($form_state)->validateFormValues($entity, $form, $form_state);
64 65 66 67 68 69 70 71 72 73

    // @todo Remove this.
    // Execute legacy global validation handlers.
    unset($form_state['validate_handlers']);
    form_execute_handlers('validate', $form, $form_state);
  }

  /**
   * Initialize the form state and the entity before the first form build.
   */
74
  protected function init(FormStateInterface $form_state) {
75 76
    // Ensure we act on the translation object corresponding to the current form
    // language.
77
    $langcode = $this->getFormLangcode($form_state);
78
    $this->entity = $this->entity->getTranslation($langcode);
79 80 81 82

    $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation());
    $this->setFormDisplay($form_display, $form_state);

83
    parent::init($form_state);
84 85
  }

86 87 88
  /**
   * {@inheritdoc}
   */
89
  public function getFormLangcode(FormStateInterface $form_state) {
90 91 92 93 94
    if (empty($form_state['langcode'])) {
      // Imply a 'view' operation to ensure users edit entities in the same
      // language they are displayed. This allows to keep contextual editing
      // working also for multilingual entities.
      $form_state['langcode'] = $this->entityManager->getTranslationFromContext($this->entity)->language()->id;
95
    }
96
    return $form_state['langcode'];
97 98 99 100 101
  }

  /**
   * {@inheritdoc}
   */
102
  public function isDefaultFormLangcode(FormStateInterface $form_state) {
103 104 105 106 107 108
    return $this->getFormLangcode($form_state) == $this->entity->getUntranslated()->language()->id;
  }

  /**
   * {@inheritdoc}
   */
109
  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
110 111 112 113 114 115
    // First, extract values from widgets.
    $extracted = $this->getFormDisplay($form_state)->extractFormValues($entity, $form, $form_state);

    // Then extract the values of fields that are not rendered through widgets,
    // by simply copying from top-level form values. This leaves the fields
    // that are not being edited within this form untouched.
116
    foreach ($form_state->getValues() as $name => $values) {
117 118
      if ($entity->hasField($name) && !isset($extracted[$name])) {
        $entity->set($name, $values);
119 120
      }
    }
121
  }
122

123 124 125
  /**
   * {@inheritdoc}
   */
126
  public function getFormDisplay(FormStateInterface $form_state) {
127 128
    return isset($form_state['form_display']) ? $form_state['form_display'] : NULL;
  }
129

130 131 132
  /**
   * {@inheritdoc}
   */
133
  public function setFormDisplay(EntityFormDisplayInterface $form_display, FormStateInterface $form_state) {
134 135
    $form_state['form_display'] = $form_display;
    return $this;
136
  }
137

138
}