Change record status: 
Project: 
Introduced in branch: 
8.0.x
Introduced in version: 
8.0.0-beta7
Description: 

Those two hooks have been removed. They were only invoked in forms, any validation/processing done there was not applied to nodes created through REST or other APIs.

hook_node_validate()

The entity validation API should be used to validate values on a node, like any other content entity. While that was already possible to define validation constraints for base fields and for a field type, there was no API to add constraints to a specific field. This has been addressed.

7.x

function mymodule_node_validate($node, $form) {
  $value = $node->myfield[LANGUAGE_NONE][0]['value'];
  if ($value < 0 && $value > 32) {
    form_set_error('myfield', t('My field value is not in the allowed range.'));
  }
}

8.x

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function mymodule_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() == 'node' && !empty($fields['myfield'])) {
    $fields['myfield']->setPropertyConstraints('value', [
      'Range' => [
        'min' => 0,
        'max' => 32,
      ],
    ]);
  }
}

Custom validation constraints can also be defined, see ForumLeafConstraint/ForumLeafConstraintValidator as an example.

hook_node_submit()

Instead of the submit hook, an #entity_builder callback should be used to map form values to an entity (or better; use a field with a widget). If the submit hook is not about mapping values to the entity, register a custom #submit callback on the submit button and put your logic there.

7.x

function mymodule_node_submit($node, $form, &$form_state) {
  // Decompose the selected menu parent option into 'menu_name' and 'plid', if
  // the form used the default parent selection widget.
  if (!empty($form_state['values']['menu']['parent'])) {
    list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $form_state['values']['menu']['parent']);
  }
}

8.x

// Implements HOOK_form_BASE_FORM_ID_alter
function mymodule_form_node_form_alter(&$form, FormStateInterface $form_state) {
  // For entity builders.
  $form['#entity_builders'][] = 'mymodule_node_builder';
  // For submit callbacks.
  $form['actions']['submit']['#submit'][] = 'mymodule_node_form_submit';
}

function mymodule_node_builder($entity_type, NodeInterface $node, &$form, FormStateInterface $form_state) {
  $parent = $form_state->getValue('menu', 'parent');
  if ($parent) {
    list($node->menu['menu_name'], $node->menu['plid']) = explode(':', $parent);
  }
}

function mymodule_node_form_submit($form, FormStateInterface $form_state) {
  $node = $form_state->getFormObject()->getEntity();
  mymodule_do_something($node);
}
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done
Progress: 
there are 2 "(" , one need to be deleted

Comments

g089h515r806’s picture

Following code does not work:



<?php
/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function mymodule_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() == 'node' && !empty($fields['myfield'])) {
    $fields['myfield']->setPropertyConstraints('value', [
      'Range' => [
        'min' => 0,
        'max' => 32,
      ],
    ]);
  }
}
?>

Chinese drupal tutorials Think in Drupal