EntityFormInterface.php 4.06 KB
Newer Older
1 2
<?php

3
namespace Drupal\Core\Entity;
4

5
use Drupal\Core\Extension\ModuleHandlerInterface;
6
use Drupal\Core\Form\BaseFormIdInterface;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Routing\RouteMatchInterface;
9
use Drupal\Core\StringTranslation\TranslationInterface;
10

11
/**
12
 * Defines an interface for entity form classes.
13
 */
14
interface EntityFormInterface extends BaseFormIdInterface {
15

16 17 18 19 20 21
  /**
   * Sets the operation for this form.
   *
   * @param string $operation
   *   The name of the current operation.
   *
22
   * @return $this
23 24 25
   */
  public function setOperation($operation);

26
  /**
27
   * Gets the operation identifying the form.
28 29 30 31 32 33 34
   *
   * @return string
   *   The name of the operation.
   */
  public function getOperation();

  /**
35
   * Gets the form entity.
36 37 38
   *
   * The form entity which has been used for populating form element defaults.
   *
39
   * @return \Drupal\Core\Entity\EntityInterface
40 41
   *   The current form entity.
   */
42
  public function getEntity();
43 44 45 46 47 48

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

60 61 62 63 64 65 66 67 68 69 70 71 72
  /**
   * 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);

73 74 75 76 77 78 79
  /**
   * 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.
   *
80
   * @see \Drupal\Core\Entity\EntityFormInterface::getEntity()
81 82 83
   *
   * @param array $form
   *   A nested array form elements comprising the form.
84 85
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
86
   *
87
   * @return \Drupal\Core\Entity\EntityInterface
88 89
   *   An updated copy of the form's entity object.
   */
90
  public function buildEntity(array $form, FormStateInterface $form_state);
91 92

  /**
93
   * Form submission handler for the 'save' action.
94
   *
95 96
   * Normally this method should be overridden to provide specific messages to
   * the user and redirect the form after the entity has been saved.
97 98
   *
   * @param array $form
99
   *   An associative array containing the structure of the form.
100 101
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
102 103 104
   *
   * @return int
   *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
105
   */
106
  public function save(array $form, FormStateInterface $form_state);
107

108
  /**
109
   * Sets the string translation service for this form.
110
   *
111
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
112 113
   *   The translation manager.
   *
114
   * @return $this
115
   */
116
  public function setStringTranslation(TranslationInterface $string_translation);
117 118 119 120 121 122 123

  /**
   * Sets the module handler for this form.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   *
124
   * @return $this
125 126 127
   */
  public function setModuleHandler(ModuleHandlerInterface $module_handler);

128 129 130 131 132 133 134 135 136 137
  /**
   * Sets the entity type manager for this form.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   *
   * @return $this
   */
  public function setEntityTypeManager(EntityTypeManagerInterface $entity_type_manager);

138
}