Commit a10b792f authored by catch's avatar catch

Issue #2018375 by andypost: Get rid of node_hook() and node_invoke().

parent 5356a24b
......@@ -13,7 +13,7 @@
*
* Field UI's "Manage fields" and "Manage display" pages let users re-order
* fields, but also non-field components. For nodes, these include the title
* and other elements exposed by modules through hook_form() or hook_form_alter().
* and other elements exposed by modules through hook_form_alter().
*
* Fieldable entities or modules that want to have their components supported
* should expose them using this hook. The user-defined settings (weight,
......
......@@ -632,6 +632,20 @@ function forum_form_taxonomy_term_form_alter(&$form, &$form_state, $form_id) {
* Implements hook_form_BASE_FORM_ID_alter() for node_form().
*/
function forum_form_node_form_alter(&$form, &$form_state, $form_id) {
$node = $form_state['controller']->getEntity();
if (isset($node->taxonomy_forums) && !$node->isNew()) {
$forum_terms = $node->taxonomy_forums;
// If editing, give option to leave shadows.
$shadow = (count($forum_terms) > 1);
$form['shadow'] = array(
'#type' => 'checkbox',
'#title' => t('Leave shadow copy'),
'#default_value' => $shadow,
'#description' => t('If you move this topic, you can leave a link in the old forum to the new forum.'),
);
$form['forum_tid'] = array('#type' => 'value', '#value' => $node->forum_tid);
}
if (isset($form['taxonomy_forums'])) {
$langcode = $form['taxonomy_forums']['#language'];
// Make the vocabulary required for 'real' forum-nodes.
......@@ -664,29 +678,6 @@ function forum_block_view_pre_render($elements) {
return $elements;
}
/**
* Implements hook_form().
*/
function forum_form(EntityInterface $node, &$form_state) {
$type = node_type_load($node->type);
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#default_value' => !empty($node->title) ? $node->title : '',
'#required' => TRUE, '#weight' => -5
);
if (!empty($node->nid)) {
$forum_terms = $node->taxonomy_forums;
// If editing, give option to leave shadows.
$shadow = (count($forum_terms) > 1);
$form['shadow'] = array('#type' => 'checkbox', '#title' => t('Leave shadow copy'), '#default_value' => $shadow, '#description' => t('If you move this topic, you can leave a link in the old forum to the new forum.'));
$form['forum_tid'] = array('#type' => 'value', '#value' => $node->forum_tid);
}
return $form;
}
/**
* Returns a tree of all forums for a given taxonomy term ID.
*
......
......@@ -22,46 +22,44 @@ function node_overview_types() {
foreach ($names as $key => $name) {
$type = $types[$key];
if (node_hook($type->type, 'form')) {
$row = array(theme('node_admin_overview', array('name' => $name, 'type' => $type)));
if ($field_ui && user_access('administer node fields')) {
$links['fields'] = array(
'title' => t('Manage fields'),
'href' => 'admin/structure/types/manage/' . $type->type . '/fields',
'weight' => 0,
);
}
if ($field_ui && user_access('administer node display')) {
$links['display'] = array(
'title' => t('Manage display'),
'href' => 'admin/structure/types/manage/' . $type->type . '/display',
'weight' => 5,
);
}
$links['edit'] = array(
'title' => t('Edit'),
'href' => 'admin/structure/types/manage/' . $type->type,
'weight' => 10,
$row = array(theme('node_admin_overview', array('name' => $name, 'type' => $type)));
if ($field_ui && user_access('administer node fields')) {
$links['fields'] = array(
'title' => t('Manage fields'),
'href' => 'admin/structure/types/manage/' . $type->type . '/fields',
'weight' => 0,
);
if ($type->custom) {
$links['delete'] = array(
'title' => t('Delete'),
'href' => 'admin/structure/types/manage/' . $type->type . '/delete',
'weight' => 15,
);
}
$row[] = array(
'data' => array(
'#type' => 'operations',
'#links' => $links,
),
}
if ($field_ui && user_access('administer node display')) {
$links['display'] = array(
'title' => t('Manage display'),
'href' => 'admin/structure/types/manage/' . $type->type . '/display',
'weight' => 5,
);
}
$links['edit'] = array(
'title' => t('Edit'),
'href' => 'admin/structure/types/manage/' . $type->type,
'weight' => 10,
);
$rows[] = $row;
if ($type->custom) {
$links['delete'] = array(
'title' => t('Delete'),
'href' => 'admin/structure/types/manage/' . $type->type . '/delete',
'weight' => 15,
);
}
$row[] = array(
'data' => array(
'#type' => 'operations',
'#links' => $links,
),
);
$rows[] = $row;
}
$build['node_table'] = array(
......
......@@ -20,8 +20,8 @@ class NodeFormController extends EntityFormController {
/**
* Prepares the node object.
*
* Fills in a few default values, and then invokes hook_prepare() on the node
* type module, and hook_node_prepare() on all modules.
* Fills in a few default values, and then invokes hook_node_prepare() on all
* modules.
*
* Overrides Drupal\Core\Entity\EntityFormController::prepareEntity().
*/
......@@ -49,7 +49,6 @@ protected function prepareEntity() {
// Always use the default revision setting.
$node->setNewRevision(in_array('revision', $node_options));
node_invoke($node, 'prepare');
module_invoke_all('node_prepare', $node);
}
......@@ -93,18 +92,16 @@ public function form(array $form, array &$form_state) {
'#default_value' => isset($node->changed) ? $node->changed : NULL,
);
// Invoke hook_form() to get the node-specific bits. Can't use node_invoke()
// because hook_form() needs to be able to receive $form_state by reference.
// @todo hook_form() implementations are unable to add #validate or #submit
// handlers to the form buttons below. Remove hook_form() entirely.
$function = node_hook($node->type, 'form');
if ($function && ($extra = $function($node, $form_state))) {
$form = NestedArray::mergeDeep($form, $extra);
}
// If the node type has a title, and the node type form defined no special
// weight for it, we default to a weight of -5 for consistency.
if (isset($form['title']) && !isset($form['title']['#weight'])) {
$form['title']['#weight'] = -5;
$node_type = node_type_load($node->type);
if ($node_type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($node_type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#maxlength' => 255,
'#weight' => -5,
);
}
$language_configuration = module_invoke('language', 'get_default_configuration', 'node', $node->type);
......@@ -342,13 +339,9 @@ public function validate(array $form, array &$form_state) {
form_set_error('date', t('You have to specify a valid date.'));
}
// Invoke hook_validate() for node type specific validation and
// hook_node_validate() for miscellaneous validation needed by modules.
// Can't use node_invoke() or module_invoke_all(), because $form_state must
// Invoke hook_node_validate() for validation needed by modules.
// Can't use module_invoke_all(), because $form_state must
// be receivable by reference.
if ($function = node_hook($node->type, 'validate')) {
$function($node, $form, $form_state);
}
foreach (module_implements('node_validate') as $module) {
$function = $module . '_node_validate';
$function($node, $form, $form_state);
......
......@@ -34,12 +34,6 @@ public function buildContent(array $entities, array $displays, $view_mode, $lang
$bundle = $entity->bundle();
$display = $displays[$bundle];
// The 'view' hook can be implemented to overwrite the default function
// to display nodes.
if (node_hook($bundle, 'view')) {
$entity = node_invoke($entity, 'view', $display, $view_mode, $langcode);
}
$entity->content['links'] = array(
'#theme' => 'links__node',
'#pre_render' => array('drupal_pre_render_links'),
......
......@@ -52,14 +52,6 @@ protected function attachLoad(&$queried_entities, $load_revision = FALSE) {
field_attach_load($this->entityType, $queried_entities);
}
// Call object type specific callbacks on each typed array of nodes.
foreach ($typed_nodes as $node_type => $nodes_of_type) {
// Retrieve the node type 'base' hook implementation based on a Node in
// the type-specific stack.
if ($function = node_hook($node_type, 'load')) {
$function($nodes_of_type);
}
}
// Besides the list of nodes, pass one additional argument to
// hook_node_load(), containing a list of node types that were loaded.
$argument = array_keys($typed_nodes);
......@@ -85,15 +77,6 @@ protected function attachLoad(&$queried_entities, $load_revision = FALSE) {
protected function invokeHook($hook, EntityInterface $node) {
$node = $node->getBCEntity();
if ($hook == 'insert' || $hook == 'update') {
node_invoke($node, $hook);
}
else if ($hook == 'predelete') {
// 'delete' is triggered in 'predelete' is here to preserve hook ordering
// from Drupal 7.
node_invoke($node, 'delete');
}
// Inline parent::invokeHook() to pass on BC-entities to node-specific
// hooks.
......
This diff is collapsed.
......@@ -329,8 +329,6 @@ function node_type_get_types() {
*
* @return string|false
* The node type base or FALSE if the node type is not found.
*
* @see node_invoke()
*/
function node_type_get_base($type) {
$types = _node_types_build()->types;
......@@ -855,42 +853,6 @@ function node_rdf_mapping() {
);
}
/**
* Determines whether a node hook exists.
*
* @param string $type
* A string containing the node type.
* @param $hook
* A string containing the name of the hook.
*
* @return string|false
* A string containing the function name or FALSE if it isn't found.
*/
function node_hook($type, $hook) {
$base = node_type_get_base($type);
return module_hook($base, $hook) ? $base . '_' . $hook : FALSE;
}
/**
* Invokes a node hook.
*
* @param \Drupal\Core\Entity\EntityInterface $node
* A Node entity.
* @param $hook
* A string containing the name of the hook.
* @param $a2, $a3, $a4
* (optional) Arguments to pass on to the hook, after the $node argument. All
* default to NULL.
*
* @return
* The returned value of the invoked hook.
*/
function node_invoke(EntityInterface $node, $hook, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
if ($function = node_hook($node->type, $hook)) {
return $function($node, $a2, $a3, $a4);
}
}
/**
* Loads node entities from the database.
*
......@@ -1575,7 +1537,7 @@ function _node_revision_access(EntityInterface $node, $op = 'view', $account = N
function _node_add_access() {
$types = node_type_get_types();
foreach ($types as $type) {
if (node_hook($type->type, 'form') && node_access('create', $type->type)) {
if (node_access('create', $type->type)) {
return TRUE;
}
}
......@@ -3112,39 +3074,6 @@ function _node_access_rebuild_batch_finished($success, $results, $operations) {
* @} End of "defgroup node_access".
*/
/**
* @defgroup node_content Hook implementations for user-created content types
* @{
* Functions that implement hooks for user-created content types.
*/
/**
* Implements hook_form().
*/
function node_content_form(EntityInterface $node, $form_state) {
// @todo It is impossible to define a content type without implementing
// hook_form(). Remove this requirement.
$form = array();
$type = node_type_load($node->type);
if ($type->has_title) {
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#required' => TRUE,
'#default_value' => $node->title,
'#maxlength' => 255,
'#weight' => -5,
);
}
return $form;
}
/**
* @} End of "defgroup node_content".
*/
/**
* Implements hook_requirements().
*/
......
......@@ -27,7 +27,7 @@ function node_add_page() {
$content = array();
// Only use node types the user has access to.
foreach (node_type_get_types() as $type) {
if (node_hook($type->type, 'form') && node_access('create', $type->type)) {
if (node_access('create', $type->type)) {
$content[$type->type] = $type;
}
}
......@@ -92,9 +92,7 @@ function node_add($node_type) {
'langcode' => $langcode ? $langcode : language_default()->langcode,
))->getBCEntity();
drupal_set_title(t('Create @name', array('@name' => $node_type->name)), PASS_THROUGH);
$output = entity_get_form($node);
return $output;
return entity_get_form($node);
}
/**
......
......@@ -910,7 +910,7 @@ function search_expression_insert($expression, $option, $value = NULL) {
* - Specifically for searching nodes, you can implement
* hook_node_update_index() and hook_node_search_result(). However, note that
* the search system already indexes all visible output of a node; i.e.,
* everything displayed normally by hook_view() and hook_node_view(). This is
* everything displayed normally by hook_node_view(). This is
* usually sufficient. You should only use this mechanism if you want
* additional, non-visible data to be indexed.
* - Implement hook_search_info(). This will create a search tab for your
......
......@@ -259,7 +259,7 @@ function hook_queue_info_alter(&$queues) {
*
* This hook allows modules to declare their own form element types and to
* specify their default values. The values returned by this hook will be
* merged with the elements returned by hook_form() implementations and so
* merged with the elements returned by form constructor implementations and so
* can return defaults for any Form APIs keys in addition to those explicitly
* mentioned below.
*
......
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