Commit 824fef8a authored by catch's avatar catch

Issue #2406103 by Berdir, alexpott: Remove hook_node_validate() and...

Issue #2406103 by Berdir, alexpott: Remove hook_node_validate() and hook_node_submit() because they bypass the entity API
parent 58bd271b
This diff is collapsed.
......@@ -408,65 +408,6 @@ function hook_node_update_index(\Drupal\node\NodeInterface $node, $langcode) {
return $text;
}
/**
* Perform node validation before a node is created or updated.
*
* This hook is invoked from NodeForm::validate(), after a user has
* finished editing the node and is previewing or submitting it. It is invoked
* at the end of all the standard validation steps.
*
* To indicate a validation error, use $form_state->setErrorByName().
*
* Note: Changes made to the $node object within your hook implementation will
* have no effect. The preferred method to change a node's content is to use
* hook_node_presave() instead. If it is really necessary to change the node at
* the validate stage, you can use setValueForElement().
*
* @param \Drupal\node\NodeInterface $node
* The node being validated.
* @param $form
* The form being used to edit the node.
* @param $form_state
* The current state of the form.
*
* @ingroup entity_crud
*/
function hook_node_validate(\Drupal\node\NodeInterface $node, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
if (isset($node->end) && isset($node->start)) {
if ($node->start > $node->end) {
$form_state->setErrorByName('time', t('An event may not end before it starts.'));
}
}
}
/**
* Act on a node after validated form values have been copied to it.
*
* This hook is invoked when a node form is submitted with either the "Save" or
* "Preview" button, after form values have been copied to the form state's node
* object, but before the node is saved or previewed. It is a chance for modules
* to adjust the node's properties from what they are simply after a copy from
* $form_state->getValues(). This hook is intended for adjusting non-field-related
* properties.
*
* @param \Drupal\node\NodeInterface $node
* The node entity being updated in response to a form submission.
* @param $form
* The form being used to edit the node.
* @param $form_state
* The current state of the form.
*
* @ingroup entity_crud
*/
function hook_node_submit(\Drupal\node\NodeInterface $node, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
// Decompose the selected menu parent option into 'menu_name' and 'parent', if
// the form used the default parent selection widget.
$parent = $form_state->getValue(array('menu', 'parent'));
if (!empty($parent)) {
list($node->menu['menu_name'], $node->menu['parent']) = explode(':', $parent);
}
}
/**
* Provide additional methods of scoring for core search results for nodes.
*
......
......@@ -293,14 +293,6 @@ public function validate(array $form, FormStateInterface $form_state) {
$form_state->setErrorByName('changed', $this->t('The content on this page has either been modified by another user, or you have already submitted modifications using this form. As a result, your changes cannot be saved.'));
}
// Invoke hook_node_validate() for validation needed by modules.
// Can't use \Drupal::moduleHandler()->invokeAll(), because $form_state must
// be receivable by reference.
foreach (\Drupal::moduleHandler()->getImplementations('node_validate') as $module) {
$function = $module . '_node_validate';
$function($node, $form, $form_state);
}
parent::validate($form, $form_state);
}
......@@ -328,12 +320,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
else {
$node->setNewRevision(FALSE);
}
$node->validated = TRUE;
foreach (\Drupal::moduleHandler()->getImplementations('node_submit') as $module) {
$function = $module . '_node_submit';
$function($node, $form, $form_state);
}
}
/**
......
......@@ -63,8 +63,7 @@ function testImport() {
$node = entity_create('node', $node);
$node->enforceIsNew();
// Verify that node_submit did not overwrite the user ID.
$this->assertEqual($node->getOwnerId(), $this->webUser->id(), 'Function node_submit() preserves user ID');
$this->assertEqual($node->getOwnerId(), $this->webUser->id());
$node->save();
// Test the import.
......
......@@ -150,10 +150,6 @@
* - hook_ENTITY_TYPE_prepare_form()
* - hook_entity_form_display_alter() (for content entities only)
*
* Some specific entity types have additional hooks that are run during
* various steps in the process:
* - Node entities: hook_node_validate() and hook_submit().
*
* @section delete Delete operations
* To delete one or more entities, load them and then delete them:
* @code
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment