EntityFormInterface.php 4.52 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Entity\EntityFormInterface.
6 7
 */

8
namespace Drupal\Core\Entity;
9

10
use Drupal\Core\Extension\ModuleHandlerInterface;
11
use Drupal\Core\Form\BaseFormIdInterface;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Routing\RouteMatchInterface;
14
use Drupal\Core\StringTranslation\TranslationInterface;
15

16
/**
17
 * Defines an interface for entity form classes.
18
 */
19
interface EntityFormInterface extends BaseFormIdInterface {
20

21 22 23 24 25 26
  /**
   * Sets the operation for this form.
   *
   * @param string $operation
   *   The name of the current operation.
   *
27
   * @return $this
28 29 30
   */
  public function setOperation($operation);

31
  /**
32
   * Returns the operation identifying the form.
33 34 35 36 37 38 39 40 41 42 43
   *
   * @return string
   *   The name of the operation.
   */
  public function getOperation();

  /**
   * Returns the form entity.
   *
   * The form entity which has been used for populating form element defaults.
   *
44
   * @return \Drupal\Core\Entity\EntityInterface
45 46
   *   The current form entity.
   */
47
  public function getEntity();
48 49 50 51 52 53

  /**
   * Sets the form entity.
   *
   * Sets the form entity which will be used for populating form element
   * defaults. Usually, the form entity gets updated by
54
   * \Drupal\Core\Entity\EntityFormInterface::submit(), however this may
55 56 57
   * be used to completely exchange the form entity, e.g. when preparing the
   * rebuild of a multi-step form.
   *
58
   * @param \Drupal\Core\Entity\EntityInterface $entity
59
   *   The entity the current form should operate upon.
60 61
   *
   * @return $this
62
   */
63
  public function setEntity(EntityInterface $entity);
64

65 66 67 68 69 70 71 72 73 74 75 76 77
  /**
   * Determines which entity will be used by this form from a RouteMatch object.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   * @param string $entity_type_id
   *   The entity type identifier.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The entity object as determined from the passed-in route match.
   */
  public function getEntityFromRouteMatch(RouteMatchInterface $route_match, $entity_type_id);

78 79 80 81 82 83 84
  /**
   * Builds an updated entity object based upon the submitted form values.
   *
   * For building the updated entity object the form's entity is cloned and
   * the submitted form values are copied to entity properties. The form's
   * entity remains unchanged.
   *
85
   * @see \Drupal\Core\Entity\EntityFormInterface::getEntity()
86 87 88
   *
   * @param array $form
   *   A nested array form elements comprising the form.
89 90
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
91
   *
92
   * @return \Drupal\Core\Entity\EntityInterface
93 94
   *   An updated copy of the form's entity object.
   */
95
  public function buildEntity(array $form, FormStateInterface $form_state);
96 97 98 99 100 101

  /**
   * Validates the submitted form values of the entity form.
   *
   * @param array $form
   *   A nested array form elements comprising the form.
102 103
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
104 105 106
   *
   * @return \Drupal\Core\Entity\ContentEntityTypeInterface
   *   The built entity.
107
   */
108
  public function validate(array $form, FormStateInterface $form_state);
109 110

  /**
111
   * Form submission handler for the 'save' action.
112
   *
113 114
   * Normally this method should be overridden to provide specific messages to
   * the user and redirect the form after the entity has been saved.
115 116
   *
   * @param array $form
117
   *   An associative array containing the structure of the form.
118 119
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
120 121 122
   *
   * @return int
   *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
123
   */
124
  public function save(array $form, FormStateInterface $form_state);
125

126
  /**
127
   * Sets the string translation service for this form.
128
   *
129
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
130 131
   *   The translation manager.
   *
132
   * @return $this
133
   */
134
  public function setStringTranslation(TranslationInterface $string_translation);
135 136 137 138 139 140 141

  /**
   * Sets the module handler for this form.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   *
142
   * @return $this
143 144 145
   */
  public function setModuleHandler(ModuleHandlerInterface $module_handler);

146 147 148 149 150 151 152 153 154 155
  /**
   * Sets the entity manager for this form.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
   *
   * @return $this
   */
  public function setEntityManager(EntityManagerInterface $entity_manager);

156
}