Same name and namespace in other branches
- 8.9.x core/modules/node/src/NodeForm.php \Drupal\node\NodeForm
- 9 core/modules/node/src/NodeForm.php \Drupal\node\NodeForm
Form handler for the node edit forms.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\node\NodeForm
- class \Drupal\Core\Entity\ContentEntityForm implements ContentEntityFormInterface
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of NodeForm
1 file declares its use of NodeForm
- TestNodeFormOverride.php in core/
modules/ media_library/ tests/ modules/ media_library_test/ src/ Form/ TestNodeFormOverride.php
File
- core/
modules/ node/ src/ NodeForm.php, line 20
Namespace
Drupal\nodeView source
class NodeForm extends ContentEntityForm {
/**
* The tempstore factory.
*
* @var \Drupal\Core\TempStore\PrivateTempStoreFactory
*/
protected $tempStoreFactory;
/**
* The Current User object.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* Constructs a NodeForm object.
*
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
* The factory for the temp store object.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
* The entity type bundle service.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
* The date formatter service.
*/
public function __construct(EntityRepositoryInterface $entity_repository, PrivateTempStoreFactory $temp_store_factory, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL, TimeInterface $time = NULL, AccountInterface $current_user, DateFormatterInterface $date_formatter) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->tempStoreFactory = $temp_store_factory;
$this->currentUser = $current_user;
$this->dateFormatter = $date_formatter;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('tempstore.private'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('current_user'), $container
->get('date.formatter'));
}
/**
* {@inheritdoc}
*/
public function form(array $form, FormStateInterface $form_state) {
// Try to restore from temp store, this must be done before calling
// parent::form().
$store = $this->tempStoreFactory
->get('node_preview');
// Attempt to load from preview when the uuid is present unless we are
// rebuilding the form.
$request_uuid = \Drupal::request()->query
->get('uuid');
if (!$form_state
->isRebuilding() && $request_uuid && ($preview = $store
->get($request_uuid))) {
/** @var \Drupal\Core\Form\FormStateInterface $preview */
$form_state
->setStorage($preview
->getStorage());
$form_state
->setUserInput($preview
->getUserInput());
// Rebuild the form.
$form_state
->setRebuild();
// The combination of having user input and rebuilding the form means
// that it will attempt to cache the form state which will fail if it is
// a GET request.
$form_state
->setRequestMethod('POST');
$this->entity = $preview
->getFormObject()
->getEntity();
$this->entity->in_preview = NULL;
$form_state
->set('has_been_previewed', TRUE);
}
/** @var \Drupal\node\NodeInterface $node */
$node = $this->entity;
if ($this->operation == 'edit') {
$form['#title'] = $this
->t('<em>Edit @type</em> @title', [
'@type' => node_get_type_label($node),
'@title' => $node
->label(),
]);
}
// Changed must be sent to the client, for later overwrite error checking.
$form['changed'] = [
'#type' => 'hidden',
'#default_value' => $node
->getChangedTime(),
];
$form = parent::form($form, $form_state);
$form['advanced']['#attributes']['class'][] = 'entity-meta';
$form['meta'] = [
'#type' => 'details',
'#group' => 'advanced',
'#weight' => -10,
'#title' => $this
->t('Status'),
'#attributes' => [
'class' => [
'entity-meta__header',
],
],
'#tree' => TRUE,
'#access' => $this->currentUser
->hasPermission('administer nodes'),
];
$form['meta']['published'] = [
'#type' => 'item',
'#markup' => $node
->isPublished() ? $this
->t('Published') : $this
->t('Not published'),
'#access' => !$node
->isNew(),
'#wrapper_attributes' => [
'class' => [
'entity-meta__title',
],
],
];
$form['meta']['changed'] = [
'#type' => 'item',
'#title' => $this
->t('Last saved'),
'#markup' => !$node
->isNew() ? $this->dateFormatter
->format($node
->getChangedTime(), 'short') : $this
->t('Not saved yet'),
'#wrapper_attributes' => [
'class' => [
'entity-meta__last-saved',
],
],
];
$form['meta']['author'] = [
'#type' => 'item',
'#title' => $this
->t('Author'),
'#markup' => $node
->getOwner()
->getAccountName(),
'#wrapper_attributes' => [
'class' => [
'entity-meta__author',
],
],
];
$form['status']['#group'] = 'footer';
// Node author information for administrators.
$form['author'] = [
'#type' => 'details',
'#title' => $this
->t('Authoring information'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'node-form-author',
],
],
'#attached' => [
'library' => [
'node/drupal.node',
],
],
'#weight' => 90,
'#optional' => TRUE,
];
if (isset($form['uid'])) {
$form['uid']['#group'] = 'author';
}
if (isset($form['created'])) {
$form['created']['#group'] = 'author';
}
// Node options for administrators.
$form['options'] = [
'#type' => 'details',
'#title' => $this
->t('Promotion options'),
'#group' => 'advanced',
'#attributes' => [
'class' => [
'node-form-options',
],
],
'#attached' => [
'library' => [
'node/drupal.node',
],
],
'#weight' => 95,
'#optional' => TRUE,
];
if (isset($form['promote'])) {
$form['promote']['#group'] = 'options';
}
if (isset($form['sticky'])) {
$form['sticky']['#group'] = 'options';
}
$form['#attached']['library'][] = 'node/form';
return $form;
}
/**
* {@inheritdoc}
*/
protected function actions(array $form, FormStateInterface $form_state) {
$element = parent::actions($form, $form_state);
$node = $this->entity;
$preview_mode = $node->type->entity
->getPreviewMode();
$element['submit']['#access'] = $preview_mode != DRUPAL_REQUIRED || $form_state
->get('has_been_previewed');
$element['preview'] = [
'#type' => 'submit',
'#access' => $preview_mode != DRUPAL_DISABLED && ($node
->access('create') || $node
->access('update')),
'#value' => $this
->t('Preview'),
'#weight' => 20,
'#submit' => [
'::submitForm',
'::preview',
],
];
if (array_key_exists('delete', $element)) {
$element['delete']['#weight'] = 100;
}
return $element;
}
/**
* Form submission handler for the 'preview' action.
*
* @param $form
* An associative array containing the structure of the form.
* @param $form_state
* The current state of the form.
*/
public function preview(array $form, FormStateInterface $form_state) {
$store = $this->tempStoreFactory
->get('node_preview');
$this->entity->in_preview = TRUE;
$store
->set($this->entity
->uuid(), $form_state);
$route_parameters = [
'node_preview' => $this->entity
->uuid(),
'view_mode_id' => 'full',
];
$options = [];
$query = $this
->getRequest()->query;
if ($query
->has('destination')) {
$options['query']['destination'] = $query
->get('destination');
$query
->remove('destination');
}
$form_state
->setRedirect('entity.node.preview', $route_parameters, $options);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$node = $this->entity;
$insert = $node
->isNew();
$node
->save();
$node_link = $node
->toLink($this
->t('View'))
->toString();
$context = [
'@type' => $node
->getType(),
'%title' => $node
->label(),
'link' => $node_link,
];
$t_args = [
'@type' => node_get_type_label($node),
'%title' => $node
->toLink()
->toString(),
];
if ($insert) {
$this
->logger('content')
->info('@type: added %title.', $context);
$this
->messenger()
->addStatus($this
->t('@type %title has been created.', $t_args));
}
else {
$this
->logger('content')
->info('@type: updated %title.', $context);
$this
->messenger()
->addStatus($this
->t('@type %title has been updated.', $t_args));
}
if ($node
->id()) {
$form_state
->setValue('nid', $node
->id());
$form_state
->set('nid', $node
->id());
if ($node
->access('view')) {
$form_state
->setRedirect('entity.node.canonical', [
'node' => $node
->id(),
]);
}
else {
$form_state
->setRedirect('<front>');
}
// Remove the preview entry from the temp store, if any.
$store = $this->tempStoreFactory
->get('node_preview');
$store
->delete($node
->uuid());
}
else {
// In the unlikely case something went wrong on save, the node will be
// rebuilt and node form redisplayed the same way as in preview.
$this
->messenger()
->addError($this
->t('The post could not be saved.'));
$form_state
->setRebuild();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
ContentEntityForm:: |
protected | function | Add revision form fields if the entity enabled the UI. | |
ContentEntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityForm:: |
|
ContentEntityForm:: |
public | function |
Button-level validation handlers are highly discouraged for entity forms,
as they will prevent entity validation from running. If the entity is going
to be saved during the form submission, this method should be manually
invoked from the button-levelā¦ Overrides FormBase:: |
2 |
ContentEntityForm:: |
public | function |
Checks whether the current form language matches the entity one. Overrides ContentEntityFormInterface:: |
|
ContentEntityForm:: |
protected | function | Checks whether the revision form fields should be added to the form. | |
NodeForm:: |
public | function |
Constructs a NodeForm object. Overrides ContentEntityForm:: |
|
ContentEntityForm:: |
protected | function |
Copies top-level form values to entity properties. Overrides EntityForm:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
ContentEntityForm:: |
protected | function | Flags violations for the current form. | |
EntityForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
NodeForm:: |
public | function | Form submission handler for the 'preview' action. | |
NodeForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
NodeForm:: |
public | function |
Gets the actual form array to be built. Overrides ContentEntityForm:: |
|
ContentEntityForm:: |
public | function |
Gets the code identifying the active form language. Overrides ContentEntityFormInterface:: |
|
FormBase:: |
protected | function | Gets the config factory for this form. | 2 |
FormBase:: |
protected | function | Gets the current user. | |
ContentEntityForm:: |
public | function |
Gets the form display. Overrides ContentEntityFormInterface:: |
|
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
MessengerTrait:: |
public | function | Gets the messenger. | 8 |
ContentEntityForm:: |
protected | function | Gets the names of all fields edited in the form. | |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
ContentEntityForm:: |
protected | function | Initializes form language code values. | |
ContentEntityForm:: |
protected | function |
Initializes the form state and the entity before the first form build. Overrides EntityForm:: |
1 |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
NodeForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ContentEntityForm:: |
|
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
ContentEntityForm:: |
protected | function |
Prepares the entity object before the form is built first. Overrides EntityForm:: |
1 |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
4 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
12 |
NodeForm:: |
protected | function |
Returns an array of supported actions for the current entity form. Overrides EntityForm:: |
|
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
ContentEntityForm:: |
protected | function | Returns the bundle entity of the entity, or NULL if there is none. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
ContentEntityForm:: |
public | function |
Sets the form display. Overrides ContentEntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
MessengerTrait:: |
public | function | Sets the messenger. | |
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
ContentEntityForm:: |
protected | function | Should new revisions created on default. | |
FormBase:: |
protected | property | The config factory. | 2 |
NodeForm:: |
protected | property | The Current User object. | |
NodeForm:: |
protected | property | The date formatter service. | |
ContentEntityForm:: |
protected | property |
The entity being used by this form. Overrides EntityForm:: |
7 |
ContentEntityForm:: |
protected | property | The entity repository service. | |
ContentEntityForm:: |
protected | property | The entity type bundle info service. | |
EntityForm:: |
protected | property | The entity type manager. | 3 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
MessengerTrait:: |
protected | property | The messenger. | 8 |
EntityForm:: |
protected | property | The module handler service. | 2 |
EntityForm:: |
protected | property | The name of the current operation. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
NodeForm:: |
protected | property | The tempstore factory. | |
ContentEntityForm:: |
protected | property | The time service. | |
ContentEntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form stateā¦ Overrides EntityForm:: |
2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ContentEntityForm:: |
public | function | Updates the changed time of the entity. | |
ContentEntityForm:: |
public | function | Updates the form language to reflect any change to the entity language. |