Rebuild permissions.', array('@node_access_rebuild' => drupal\url('admin/reports/status/rebuild'))); } drupal\set_message($message, 'error'); } switch ($path) { case 'admin/help#node': $output .= drupal\t("
The Node module manages the creation, editing, deletion, settings, and display of the main site content. Content items managed by the Node module are typically displayed as pages on your site, and include a title, some meta-data (author, creation time, content type, etc.), and optional fields containing text or other data (fields are managed by the Field module). For more information, see the online handbook entry for Node module.
", array( '@node' => 'http://drupal.org/handbook/modules/node', '@field' => drupal\url('admin/help/field')) ); $output .= '' . drupal\t('Individual content types can have different fields, behaviors, and permissions assigned to them.') . '
'; case 'admin/structure/types/manage/%/display': return '' . drupal\t('Content items can be displayed using different view modes: Teaser, Full content, Print, RSS, etc. Teaser is a short format that is typically used in lists of multiple content items. Full content is typically used when the content is displayed on its own page.') . '
' . '' . drupal\t('Here, you can define which fields are shown and hidden when %type content is displayed in each view mode, and define how the fields are displayed in each view mode.', array('%type' => type_get_name($arg[4]))) . '
'; case 'node/%/revisions': return '' . drupal\t('Revisions allow you to track differences between multiple versions of your content, and revert back to older versions.') . '
'; case 'node/%/edit': $node = load($arg[1]); $type = type_get_type($node); return (!empty($type->help) ? '' . drupal\filter_xss_admin($type->help) . '
' : ''); } if ($arg[0] == 'node' && $arg[1] == 'add' && $arg[2]) { $type = type_get_type(str_replace('-', '_', $arg[2])); return (!empty($type->help) ? '' . drupal\filter_xss_admin($type->help) . '
' : ''); } } /** * @implements system__theme() */ function system__theme() { return array( 'node' => array( 'render element' => 'elements', 'template' => 'node', ), 'search_admin' => array( 'render element' => 'form', ), 'add_list' => array( 'variables' => array('content' => NULL), 'file' => 'node.pages.inc', ), 'preview' => array( 'variables' => array('node' => NULL), 'file' => 'node.pages.inc', ), 'admin_overview' => array( 'variables' => array('name' => NULL, 'type' => NULL), ), 'recent_block' => array( 'variables' => array('nodes' => NULL), ), 'recent_content' => array( 'variables' => array('node' => NULL), ), ); } /** * @implements system__cron() */ function system__cron() { db\delete('history') ->condition('timestamp', NODE_NEW_LIMIT, '<') ->execute(); } /** * @implements entity__info() */ function entity__info() { $return = array( 'node' => array( 'label' => drupal\t('Node'), 'controller class' => 'NodeController', 'base table' => 'node', 'revision table' => 'node_revision', 'uri callback' => 'node_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'nid', 'revision' => 'vid', 'bundle' => 'type', 'label' => 'title', ), 'bundle keys' => array( 'bundle' => 'type', ), 'bundles' => array(), 'view modes' => array( 'full' => array( 'label' => drupal\t('Full content'), 'custom settings' => FALSE, ), 'teaser' => array( 'label' => drupal\t('Teaser'), 'custom settings' => TRUE, ), 'rss' => array( 'label' => drupal\t('RSS'), 'custom settings' => FALSE, ), ), ), ); // Search integration is provided by node.module, so search-related // view modes for nodes are defined here and not in search.module. if (drupal\module_exists('search')) { $return['node']['view modes'] += array( 'search_index' => array( 'label' => drupal\t('Search index'), 'custom settings' => FALSE, ), 'search_result' => array( 'label' => drupal\t('Search result'), 'custom settings' => FALSE, ), ); } // Bundles must provide a human readable name so we can create help and error // messages, and the path to attach Field admin pages to. foreach (type_get_names() as $type => $name) { $return['node']['bundles'][$type] = array( 'label' => $name, 'admin' => array( 'path' => 'admin/structure/types/manage/%node_type', 'real path' => 'admin/structure/types/manage/' . str_replace('_', '-', $type), 'bundle argument' => 4, 'access arguments' => array('administer content types'), ), ); } return $return; } /** * @implements field__display_alter__ENTITY_TYPE() */ function field__display_alter__node(&$display, $context) { // Hide field labels in search index. if ($context['view_mode'] == 'search_index') { $display['label'] = 'hidden'; } } /** * Entity uri callback. */ function uri($node) { return array( 'path' => 'node/' . $node->nid, ); } /** * @implements system__admin_paths() */ function system__admin_paths() { $paths = array( 'node/*/edit' => TRUE, 'node/*/delete' => TRUE, 'node/*/revisions' => TRUE, 'node/*/revisions/*/revert' => TRUE, 'node/*/revisions/*/delete' => TRUE, 'node/add' => TRUE, 'node/add/*' => TRUE, ); return $paths; } /** * Gathers a listing of links to nodes. * * @public * * @param $result * A DB result object from a query to fetch node entities. If your query * joins thenode_comment_statistics table so that the
* comment_count field is available, a title attribute will
* be added to show the number of comments.
* @param $title
* A heading for the resulting list.
*
* @return
* A renderable array containing a list of linked node titles fetched from
* $result, or FALSE if there are no rows in $result.
*/
function title_list($result, $title = NULL) {
$items = array();
$num_rows = FALSE;
foreach ($result as $node) {
$items[] = drupal\l($node->title, 'node/' . $node->nid, !empty($node->comment_count) ? array('attributes' => array('title' => drupal\format_plural($node->comment_count, '1 comment', '@count comments'))) : array());
$num_rows = TRUE;
}
return $num_rows ? array('#theme' => 'item_list__node', '#items' => $items, '#title' => $title) : FALSE;
}
/**
* Update the 'last viewed' timestamp of the specified node for current user.
*
* @public
*
* @param $node
* A node object.
*/
function tag_new($node) {
global $user;
if ($user->uid) {
db\merge('history')
->key(array(
'uid' => $user->uid,
'nid' => $node->nid,
))
->fields(array('timestamp' => REQUEST_TIME))
->execute();
}
}
/**
* Retrieves the timestamp at which the current user last viewed the
* specified node.
*/
function last_viewed($nid) {
global $user;
$history = &drupal\static(__FUNCTION__, array());
if (!isset($history[$nid])) {
$history[$nid] = db\query("SELECT timestamp FROM {history} WHERE uid = :uid AND nid = :nid", array(':uid' => $user->uid, ':nid' => $nid))->fetchObject();
}
return (isset($history[$nid]->timestamp) ? $history[$nid]->timestamp : 0);
}
/**
* Decide on the type of marker to be displayed for a given node.
*
* @public
*
* @param $nid
* Node ID whose history supplies the "last viewed" timestamp.
* @param $timestamp
* Time which is compared against node's "last viewed" timestamp.
* @return
* One of the MARK constants.
*/
function mark($nid, $timestamp) {
global $user;
$cache = &drupal\static(__FUNCTION__, array());
if (!$user->uid) {
return MARK_READ;
}
if (!isset($cache[$nid])) {
$cache[$nid] = last_viewed($nid);
}
if ($cache[$nid] == 0 && $timestamp > NODE_NEW_LIMIT) {
return MARK_NEW;
}
elseif ($timestamp > $cache[$nid] && $timestamp > NODE_NEW_LIMIT) {
return MARK_UPDATED;
}
return MARK_READ;
}
/**
* Extract the type name.
*
* @param $node
* Either a string or object, containing the node type information.
*
* @return
* Node type of the passed in data.
*/
function extract_type($node) {
return is_object($node) ? $node->type : $node;
}
/**
* Returns a list of all the available node types.
*
* This list can include types that are queued for addition or deletion.
* See types_build() for details.
*
* @public
*
* @return
* An array of node types, as objects, keyed by the type.
*
* @see type_get_type()
*/
function type_get_types() {
return types_build()->types;
}
/**
* Returns the node type of the passed node or node type string.
*
* @public
*
* @param $node
* A node object or string that indicates the node type to return.
*
* @return
* A single node type, as an object, or FALSE if the node type is not found.
* The node type is an object containing fields from hook_node_info() return
* values, as well as the field 'type' (the machine-readable type) and other
* fields used internally and defined in types_build(),
* hook_node_info(), and node_type_set_defaults().
*/
function type_get_type($node) {
$type = extract_type($node);
$types = types_build()->types;
return isset($types[$type]) ? $types[$type] : FALSE;
}
/**
* Returns the node type base of the passed node or node type string.
*
* The base indicates which module implements this node type and is used to
* execute node-type-specific hooks. For types defined in the user interface
* and managed by node.module, the base is 'node_content'.
*
* @public
*
* @param $node
* A node object or string that indicates the node type to return.
*
* @return
* The node type base or FALSE if the node type is not found.
*
* @see node_invoke()
*/
function type_get_base($node) {
$type = extract_type($node);
$types = types_build()->types;
return isset($types[$type]) && isset($types[$type]->base) ? $types[$type]->base : FALSE;
}
/**
* Returns a list of available node type names.
*
* This list can include types that are queued for addition or deletion.
* See types_build() for details.
*
* @public
*
* @return
* An array of node type names, keyed by the type.
*/
function type_get_names() {
return types_build()->names;
}
/**
* Returns the node type name of the passed node or node type string.
*
* @public
*
* @param $node
* A node object or string that indicates the node type to return.
*
* @return
* The node type name or FALSE if the node type is not found.
*/
function type_get_name($node) {
$type = extract_type($node);
$types = types_build()->names;
return isset($types[$type]) ? $types[$type] : FALSE;
}
/**
* Updates the database cache of node types.
*
* All new module-defined node types are saved to the database via a call to
* node_type_save(), and obsolete ones are deleted via a call to
* node_type_delete(). See types_build() for an explanation of the new
* and obsolete types.
*
* @public
*/
function types_rebuild() {
types_build(TRUE);
}
/**
* Menu argument loader: loads a node type by string.
*
* @param $name
* The machine-readable name of a node type to load, where '_' is replaced
* with '-'.
*
* @return
* A node type object or FALSE if $name does not exist.
*/
function type_load($name) {
return type_get_type(strtr($name, array('-' => '_')));
}
/**
* Saves a node type to the database.
*
* @public
*
* @param $info
* The node type to save, as an object.
*
* @return
* Status flag indicating outcome of the operation.
*/
function type_save($info) {
$is_existing = FALSE;
$existing_type = !empty($info->old_type) ? $info->old_type : $info->type;
$is_existing = (bool) db\query_range('SELECT 1 FROM {node_type} WHERE type = :type', 0, 1, array(':type' => $existing_type))->fetchField();
$type = type_set_defaults($info);
$fields = array(
'type' => (string) $type->type,
'name' => (string) $type->name,
'base' => (string) $type->base,
'has_title' => (int) $type->has_title,
'title_label' => (string) $type->title_label,
'description' => (string) $type->description,
'help' => (string) $type->help,
'custom' => (int) $type->custom,
'modified' => (int) $type->modified,
'locked' => (int) $type->locked,
'disabled' => (int) $type->disabled,
'module' => $type->module,
);
if ($is_existing) {
db\update('node_type')
->fields($fields)
->condition('type', $existing_type)
->execute();
if (!empty($type->old_type) && $type->old_type != $type->type) {
field\attach_rename_bundle('node', $type->old_type, $type->type);
}
drupal\invoke_all('node', 'type_update', $type);
$status = SAVED_UPDATED;
}
else {
$fields['orig_type'] = (string) $type->orig_type;
db\insert('node_type')
->fields($fields)
->execute();
field\attach_create_bundle('node', $type->type);
drupal\invoke_all('node', 'type_insert', $type);
$status = SAVED_NEW;
}
// Clear the node type cache.
drupal\static_reset('drupal\node\types_build');
return $status;
}
/**
* Add default body field to a node type.
*
* @public
*
* @param $type
* A node type object.
* @param $label
* The label for the body instance.
*
* @return
* Body field instance.
*/
function add_body_field($type, $label = 'Body') {
// Add or remove the body field, as needed.
$field = field\info_field('body');
$instance = field\info_instance('node', 'body', $type->type);
if (empty($field)) {
$field = array(
'field_name' => 'body',
'type' => 'text_with_summary',
'entity_types' => array('node'),
'translatable' => TRUE,
);
$field = field\create_field($field);
}
if (empty($instance)) {
$instance = array(
'field_name' => 'body',
'entity_type' => 'node',
'bundle' => $type->type,
'label' => $label,
'widget_type' => 'text_textarea_with_summary',
'settings' => array('display_summary' => TRUE),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'text_default',
),
'teaser' => array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
),
),
);
$instance = field\create_instance($instance);
}
return $instance;
}
/**
* @implements field__extra_fields()
*/
function field__extra_fields() {
$extra = array();
foreach (type_get_types() as $type) {
if ($type->has_title) {
$extra['node'][$type->type] = array(
'form' => array(
'title' => array(
'label' => $type->title_label,
'description' => drupal\t("Node module element"),
'weight' => -5,
),
),
);
}
}
return $extra;
}
/**
* Deletes a node type from the database.
*
* @public
*
* @param $type
* The machine-readable name of the node type to be deleted.
*/
function type_delete($type) {
$info = type_get_type($type);
db\delete('node_type')
->condition('type', $type)
->execute();
field\attach_delete_bundle('node', $type);
drupal\invoke_all('node', 'type_delete', $info);
// Clear the node type cache.
drupal\static_reset('drupal\node\types_build');
}
/**
* Updates all nodes of one type to be of another type.
*
* @public
*
* @param $old_type
* The current node type of the nodes.
* @param $type
* The new node type of the nodes.
*
* @return
* The number of nodes whose node type field was modified.
*/
function type_update_nodes($old_type, $type) {
return db\update('node')
->fields(array('type' => $type))
->condition('type', $old_type)
->execute();
}
/**
* Builds and returns the list of available node types.
*
* The list of types is built by invoking hook_node_info() on all modules and
* comparing this information with the node types in the {node_type} table.
* These two information sources are not synchronized during module installation
* until node_types_rebuild() is called.
*
* @param $rebuild
* TRUE to rebuild node types. Equivalent to calling node_types_rebuild().
* @return
* Associative array with two components:
* - names: Associative array of the names of node types, keyed by the type.
* - types: Associative array of node type objects, keyed by the type.
* Both of these arrays will include new types that have been defined by
* hook_node_info() implementations but not yet saved in the {node_type}
* table. These are indicated in the type object by $type->is_new being set
* to the value 1. These arrays will also include obsolete types: types that
* were previously defined by modules that have now been disabled, or for
* whatever reason are no longer being defined in hook_node_info()
* implementations, but are still in the database. These are indicated in the
* type object by $type->disabled being set to TRUE.
*/
function types_build($rebuild = FALSE) {
if (!$rebuild) {
$_node_types = &drupal\static(__FUNCTION__);
if (is_object($_node_types)) {
return $_node_types;
}
}
$_node_types = (object)array('types' => array(), 'names' => array());
foreach (drupal\implements('node', 'info') as $module) {
$info_array = drupal\invoke($module, 'node', 'info');
foreach ($info_array as $type => $info) {
$info['type'] = $type;
$_node_types->types[$type] = type_set_defaults($info);
$_node_types->types[$type]->module = $module;
$_node_types->names[$type] = $info['name'];
}
}
$query = db\select('node_type', 'nt')
->addTag('translatable')
->addTag('node_type_access')
->fields('nt')
->orderBy('nt.type', 'ASC');
if (!$rebuild) {
$query->condition('disabled', 0);
}
foreach ($query->execute() as $type_object) {
$type_db = $type_object->type;
// Original disabled value.
$disabled = $type_object->disabled;
// Check for node types from disabled modules and mark their types for removal.
// Types defined by the node module in the database (rather than by a separate
// module using hook_node_info) have a base value of 'node_content'. The isset()
// check prevents errors on old (pre-Drupal 7) databases.
if (isset($type_object->base) && $type_object->base != 'node_content' && empty($info_array[$type_db])) {
$type_object->disabled = TRUE;
}
if (isset($info_array[$type_db])) {
$type_object->disabled = FALSE;
}
if (!isset($_node_types->types[$type_db]) || $type_object->modified) {
$_node_types->types[$type_db] = $type_object;
$_node_types->names[$type_db] = $type_object->name;
if ($type_db != $type_object->orig_type) {
unset($_node_types->types[$type_object->orig_type]);
unset($_node_types->names[$type_object->orig_type]);
}
}
$_node_types->types[$type_db]->disabled = $type_object->disabled;
$_node_types->types[$type_db]->disabled_changed = $disabled != $type_object->disabled;
}
if ($rebuild) {
foreach ($_node_types->types as $type => $type_object) {
if (!empty($type_object->is_new) || !empty($type_object->disabled_changed)) {
node_type_save($type_object);
}
}
}
asort($_node_types->names);
return $_node_types;
}
/**
* Set the default values for a node type.
*
* The defaults are for a type defined through hook_node_info().
* When populating a custom node type $info should have the 'custom'
* key set to 1.
*
* @public
*
* @param $info
* An object or array containing values to override the defaults.
*
* @return
* A node type object.
*/
function type_set_defaults($info = array()) {
$type = &drupal\static(__FUNCTION__);
if (!isset($type)) {
$type = new stdClass();
$type->type = '';
$type->name = '';
$type->base = '';
$type->description = '';
$type->help = '';
$type->custom = 0;
$type->modified = 0;
$type->locked = 1;
$type->disabled = 0;
$type->is_new = 1;
$type->has_title = 1;
$type->title_label = 'Title';
}
$new_type = clone $type;
$info = (array) $info;
foreach ($info as $key => $data) {
$new_type->$key = $data;
}
// If the type has no title, set an empty label.
if (!$new_type->has_title) {
$new_type->title_label = '';
}
if (empty($new_type->module)) {
$new_type->module = $new_type->base == 'node_content' ? 'node' : '';
}
$new_type->orig_type = isset($info['type']) ? $info['type'] : '';
return $new_type;
}
/**
* @implements rdf__mapping()
*/
function rdf__mapping() {
return array(
array(
'type' => 'node',
'bundle' => RDF_DEFAULT_BUNDLE,
'mapping' => array(
'rdftype' => array('sioc:Item', 'foaf:Document'),
'title' => array(
'predicates' => array('dc:title'),
),
'created' => array(
'predicates' => array('dc:date', 'dc:created'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
'changed' => array(
'predicates' => array('dc:modified'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
'body' => array(
'predicates' => array('content:encoded'),
),
'uid' => array(
'predicates' => array('sioc:has_creator'),
'type' => 'rel',
),
'name' => array(
'predicates' => array('foaf:name'),
),
'comment_count' => array(
'predicates' => array('sioc:num_replies'),
'datatype' => 'xsd:integer',
),
'last_activity' => array(
'predicates' => array('sioc:last_activity_date'),
'datatype' => 'xsd:dateTime',
'callback' => 'date_iso8601',
),
),
),
);
}
/**
* Determine whether a node hook exists.
*
* @public
*
* @param $node
* A node object or a string containing the node type.
* @param $hook
* A string containing the name of the hook.
* @return
* TRUE if the $hook exists in the node type of $node.
*/
function hook($node, $hook) {
$base = type_get_base($node);
return drupal\hook($base, 'node', $hook);
}
/**
* Invoke a node hook.
*
* @public
*
* @param $node
* A node object or a string containing the node type.
* @param $hook
* A string containing the name of the hook.
* @param $a2, $a3, $a4
* Arguments to pass on to the hook, after the $node argument.
* @return
* The returned value of the invoked hook.
*/
function invoke($node, $hook, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
if (hook($node, $hook)) {
$base = type_get_base($node);
$function = $base . '\node__' . $hook;
return ($function($node, $a2, $a3, $a4));
}
}
/**
* Load node entities from the database.
*
* This function should be used whenever you need to load more than one node
* from the database. Nodes are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @public
*
* @see entity_load()
*
* @param $nids
* An array of node IDs.
* @param $conditions
* An array of conditions on the {node} table in the form 'field' => $value.
* @param $reset
* Whether to reset the internal node_load cache.
*
* @return
* An array of node objects indexed by nid.
*/
function load_multiple($nids = array(), $conditions = array(), $reset = FALSE) {
return drupal\entity_load('node', $nids, $conditions, $reset);
}
/**
* Load a node object from the database.
*
* @public
*
* @param $nid
* The node ID.
* @param $vid
* The revision ID.
* @param $reset
* Whether to reset the load_multiple cache.
*
* @return
* A fully-populated node object.
*/
function load($nid = NULL, $vid = NULL, $reset = FALSE) {
$nids = (isset($nid) ? array($nid) : array());
$conditions = (isset($vid) ? array('vid' => $vid) : array());
$node = load_multiple($nids, $conditions, $reset);
return $node ? reset($node) : FALSE;
}
/**
* Prepares a node object for editing.
*
* Fills in a few default values, and then invokes hook_prepare() on the node
* type module, and hook_node_prepare() on all modules.
*/
function object_prepare($node) {
// Set up default values, if required.
$node_options = drupal\variable_get('node_options_' . $node->type, array('status', 'promote'));
// If this is a new node, fill in the default values.
if (!isset($node->nid) || isset($node->is_new)) {
foreach (array('status', 'promote', 'sticky') as $key) {
// Multistep node forms might have filled in something already.
if (!isset($node->$key)) {
$node->$key = (int) in_array($key, $node_options);
}
}
global $user;
$node->uid = $user->uid;
$node->created = REQUEST_TIME;
}
else {
$node->date = drupal\format_date($node->created, 'custom', 'Y-m-d H:i:s O');
// Remove the log message from the original node object.
$node->log = NULL;
}
// Always use the default revision setting.
$node->revision = in_array('revision', $node_options);
invoke($node, 'prepare');
drupal\invoke_all('node', 'prepare', $node);
}
/**
* Perform validation checks on the given node.
*/
function validate($node, $form, &$form_state) {
$type = type_get_type($node);
if (isset($node->nid) && (last_changed($node->nid) > $node->changed)) {
drupal\form_set_error('changed', drupal\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.'));
}
// Validate the "authored by" field.
if (!empty($node->name) && !($account = user\load_by_name($node->name))) {
// The use of empty() is mandatory in the context of usernames
// as the empty string denotes the anonymous user. In case we
// are dealing with an anonymous user we set the user ID to 0.
drupal\form_set_error('name', drupal\t("The username %name does not exist.", array('%name' => $node->name)));
}
// Validate the "authored on" field.
if (!empty($node->date) && strtotime($node->date) === FALSE) {
drupal\form_set_error('date', drupal\t('You have to specify a valid date.'));
}
// Invoke hook node__validate. Can't use invoke() or drupal\invoke_all(),
// because $form_state must be receivable by reference.
foreach (drupal\implements('node', 'validate') as $module) {
$function = $module . '\node__validate';
$function($node, $form, $form_state);
}
}
/**
* Prepare node for saving by populating author and creation date.
*/
function submit($node) {
global $user;
// A user might assign the node author by entering a user name in the node
// form, which we then need to translate to a user ID.
if (isset($node->name)) {
if ($account = user\load_by_name($node->name)) {
$node->uid = $account->uid;
}
else {
$node->uid = 0;
}
}
$node->created = !empty($node->date) ? strtotime($node->date) : REQUEST_TIME;
$node->validated = TRUE;
return $node;
}
/**
* Save changes to a node or add a new node.
*
* @public
*
* @param $node
* The $node object to be saved. If $node->nid is
* omitted (or $node->is_new is TRUE), a new node will be added.
*/
function save($node) {
$transaction = db\transaction();
try {
field\attach_presave('node', $node);
global $user;
// Determine if we will be inserting a new node.
if (!isset($node->is_new)) {
$node->is_new = empty($node->nid);
}
// Set the timestamp fields.
if (empty($node->created)) {
$node->created = REQUEST_TIME;
}
// The changed timestamp is always updated for bookkeeping purposes,
// for example: revisions, searching, etc.
$node->changed = REQUEST_TIME;
$node->timestamp = REQUEST_TIME;
$update_node = TRUE;
// Let modules modify the node before it is saved to the database.
drupal\invoke_all('node', 'presave', $node);
if ($node->is_new || !empty($node->revision)) {
// When inserting either a new node or a new node revision, $node->log
// must be set because {node_revision}.log is a text column and therefore
// cannot have a default value. However, it might not be set at this
// point (for example, if the user submitting a node form does not have
// permission to create revisions), so we ensure that it is at least an
// empty string in that case.
// @todo: Make the {node_revision}.log column nullable so that we can
// remove this check.
if (!isset($node->log)) {
$node->log = '';
}
}
elseif (empty($node->log)) {
// If we are updating an existing node without adding a new revision, we
// need to make sure $node->log is unset whenever it is empty. As long as
// $node->log is unset, drupal\write_record() will not attempt to update
// the existing database column when re-saving the revision; therefore,
// this code allows us to avoid clobbering an existing log entry with an
// empty one.
unset($node->log);
}
// When saving a new node revision, unset any existing $node->vid so as to
// ensure that a new revision will actually be created, then store the old
// revision ID in a separate property for use by node hook implementations.
if (!$node->is_new && !empty($node->revision) && $node->vid) {
$node->old_vid = $node->vid;
unset($node->vid);
}
// Save the node and node revision.
if ($node->is_new) {
// For new nodes, save new records for both the node itself and the node
// revision.
drupal\write_record('node', $node);
save_revision($node, $user->uid);
$op = 'insert';
}
else {
// For existing nodes, update the node record which matches the value of
// $node->nid.
drupal\write_record('node', $node, 'nid');
// Then, if a new node revision was requested, save a new record for
// that; otherwise, update the node revision record which matches the
// value of $node->vid.
if (!empty($node->revision)) {
save_revision($node, $user->uid);
}
else {
save_revision($node, $user->uid, 'vid');
$update_node = FALSE;
}
$op = 'update';
}
if ($update_node) {
db\update('node')
->fields(array('vid' => $node->vid))
->condition('nid', $node->nid)
->execute();
}
// Call the node specific callback (if any). This can be
// node_invoke($node, 'insert') or
// node_invoke($node, 'update').
invoke($node, $op);
// Save fields.
$function = "field\attach_$op";
$function('node', $node);
drupal\invoke_all('node', $op, $node);
drupal\invoke_all('entity', $op, $node, 'node');
// Update the node access table for this node. There's no need to delete
// existing records if the node is new.
$delete = $op == 'update';
access_acquire_grants($node, $delete);
// Clear internal properties.
unset($node->is_new);
// Ignore slave server temporarily to give time for the
// saved node to be propagated to the slave.
db\ignore_slave();
}
catch (Exception $e) {
$transaction->rollback('node');
drupal\watchdog_exception('node', $e);
throw $e;
}
}
/**
* Helper function to save a revision with the uid of the current user.
*
* Node is taken by reference, because drupal\write_record() updates the
* $node with the revision id, and we need to pass that back to the caller.
*/
function save_revision($node, $uid, $update = NULL) {
$temp_uid = $node->uid;
$node->uid = $uid;
if (isset($update)) {
drupal\write_record('node_revision', $node, $update);
}
else {
drupal\write_record('node_revision', $node);
}
$node->uid = $temp_uid;
}
/**
* Delete a node.
*
* @public
*
* @param $nid
* A node ID.
*/
function delete($nid) {
delete_multiple(array($nid));
}
/**
* Delete multiple nodes.
*
* @public
*
* @param $nids
* An array of node IDs.
*/
function delete_multiple($nids) {
if (!empty($nids)) {
$nodes = load_multiple($nids, array());
foreach ($nodes as $nid => $node) {
// Call the node-specific callback (if any):
invoke($node, 'delete');
drupal\invoke_all('node', 'delete', $node);
drupal\invoke_all('entity', 'delete', $node, 'node');
field\attach_delete('node', $node);
// Remove this node from the search index if needed.
// This code is implemented in node module rather than in search module,
// because node module is implementing search module's API, not the other
// way around.
if (drupal\module_exists('search')) {
search\reindex($nid, 'node');
}
}
// Delete after calling hooks so that they can query node tables as needed.
db\delete('node')
->condition('nid', $nids, 'IN')
->execute();
db\delete('node_revision')
->condition('nid', $nids, 'IN')
->execute();
db\delete('history')
->condition('nid', $nids, 'IN')
->execute();
db\delete('node_access')
->condition('nid', $nids, 'IN')
->execute();
// Clear the page and block and load_multiple caches.
drupal\cache_clear_all();
drupal\entity_get_controller('node')->resetCache();
}
}
/**
* Delete a node revision.
*
* @public
*
* @param $revision_id
* The revision ID to delete.
*/
function revision_delete($revision_id) {
if ($revision = load(NULL, $revision_id)) {
// Prevent deleting the current revision.
$node = load($revision->nid);
if ($revision_id == $node->vid) {
return FALSE;
}
db\delete('node_revision')
->condition('nid', $revision->nid)
->condition('vid', $revision->vid)
->execute();
drupal\invoke_all('node', 'revision_delete', $revision);
field\attach_delete_revision('node', $revision);
return TRUE;
}
return FALSE;
}
/**
* Generate an array for rendering the given node.
*
* @public
*
* @param $node
* A node object.
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*
* @return
* An array as expected by drupal\render().
*/
function view($node, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// Populate $node->content with a render() array.
build_content($node, $view_mode, $langcode);
$build = $node->content;
// We don't need duplicate rendering info in node->content.
unset($node->content);
$build += array(
'#theme' => 'node',
'#node' => $node,
'#view_mode' => $view_mode,
'#language' => $langcode,
);
// Add contextual links for this node, except when the node is already being
// displayed on its own page. Modules may alter this behavior (for example,
// to restrict contextual links to certain view modes) by implementing
// hook_node_view_alter().
if (!empty($node->nid) && !($view_mode == 'full' && is_page($node))) {
$build['#contextual_links']['node'] = array('node', array($node->nid));
}
// Allow modules to modify the structured node.
$type = 'node';
drupal\alter(array('node_view', 'entity_view'), $build, $type);
return $build;
}
/**
* Builds a structured array representing the node's content.
*
* @public
*
* The content built for the node (field values, comments, file attachments or
* other node components) will vary depending on the $view_mode parameter.
*
* Drupal core defines the following view modes for nodes, with the following
* default use cases:
* - full (default): node is being displayed on its own page (node/123)
* - teaser: node is being displayed on the default home page listing, on
* taxonomy listing pages, or on blog listing pages.
* - rss: node displayed in an RSS feed.
* If search.module is enabled:
* - search_index: node is being indexed for search.
* - search_result: node is being displayed as a search result.
* If book.module is enabled:
* - print: node is being displayed in print-friendly mode.
* Contributed modules might define additional view modes, or use existing
* view modes in additional contexts.
*
* @param $node
* A node object.
* @param $view_mode
* View mode, e.g. 'full', 'teaser'...
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
*/
function build_content($node, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
// Remove previously built content, if exists.
$node->content = array();
// The 'view' hook can be implemented to overwrite the default function
// to display nodes.
if (hook($node, 'view')) {
$node = invoke($node, 'view', $view_mode);
}
// Build fields content.
// In case of a multiple view, view_multiple() already ran the
// 'prepare_view' step. An internal flag prevents the operation from running
// twice.
field\attach_prepare_view('node', array($node->nid => $node), $view_mode);
drupal\entity_prepare_view('node', array($node->nid => $node));
$node->content += field\attach_view('node', $node, $view_mode, $langcode);
// Always display a read more link on teasers because we have no way
// to know when a teaser view is different than a full view.
$links = array();
if ($view_mode == 'teaser') {
$links['node-readmore'] = array(
'title' => drupal\t('Read more'),
'href' => 'node/' . $node->nid,
'attributes' => array('rel' => 'tag', 'title' => strip_tags($node->title))
);
}
$node->content['links'] = array(
'#theme' => 'links__node',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
// Allow modules to make their own additions to the node.
drupal\invoke_all('node', 'view', $node, $view_mode, $langcode);
drupal\invoke_all('entity', 'view', $node, 'node', $view_mode, $langcode);
}
/**
* Generate an array which displays a node detail page.
*
* @public
*
* @param $node
* A node object.
* @param $message
* A flag which sets a page title relevant to the revision being viewed.
* @return
* A $page element suitable for use by drupal\page_render().
*/
function show($node, $message = FALSE) {
if ($message) {
drupal\set_title(t('Revision of %title from %date', array('%title' => $node->title, '%date' => format_date($node->revision_timestamp))), PASS_THROUGH);
}
// Update the history table, stating that this user viewed this node.
tag_new($node);
// For markup consistency with other pages, use view_multiple() rather than view().
return view_multiple(array($node->nid => $node), 'full');
}
/**
* Returns whether the current page is the full page view of the passed in node.
*
* @public
*
* @param $node
* A node object.
*/
function is_page($node) {
$page_node = drupal\menu_get_object();
return (!empty($page_node) ? $page_node->nid == $node->nid : FALSE);
}
/**
* Process variables for node.tpl.php
*
* Most themes utilize their own copy of node.tpl.php. The default is located
* inside "modules/node/node.tpl.php". Look in there for the full list of
* variables.
*
* The $variables array contains the following arguments:
* - $node
* - $view_mode
* - $page
*
* @see node.tpl.php
*/
function node__theme__preprocess_node(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Provide a distinct $teaser boolean.
$variables['teaser'] = $variables['view_mode'] == 'teaser';
$variables['node'] = $variables['elements']['#node'];
$node = $variables['node'];
$variables['date'] = drupal\format_date($node->created);
$variables['name'] = drupal\theme('username', array('account' => $node));
$uri = drupal\entity_uri('node', $node);
$variables['node_url'] = drupal\url($uri['path'], $uri['options']);
$variables['title'] = drupal\check_plain($node->title);
$variables['page'] = $variables['view_mode'] == 'full' && is_page($node);
if (!empty($node->in_preview)) {
unset($node->content['links']);
}
// Flatten the node object's member fields.
$variables = array_merge((array) $node, $variables);
// Helpful $content variable for templates.
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
// Make the field variables available with the appropriate language.
field\attach_preprocess('node', $node, $variables['content'], $variables);
// Display post information only on certain node types.
if (drupal\variable_get('node_submitted_' . $node->type, TRUE)) {
$variables['display_submitted'] = TRUE;
$variables['user_picture'] = drupal\theme_get_setting('toggle_node_user_picture') ? drupal\theme('user_picture', array('account' => $node)) : '';
}
else {
$variables['display_submitted'] = FALSE;
$variables['user_picture'] = '';
}
// Gather node classes.
$variables['classes_array'][] = drupal\html_class('node-' . $node->type);
if ($variables['promote']) {
$variables['classes_array'][] = 'node-promoted';
}
if ($variables['sticky']) {
$variables['classes_array'][] = 'node-sticky';
}
if (!$variables['status']) {
$variables['classes_array'][] = 'node-unpublished';
}
if ($variables['teaser']) {
$variables['classes_array'][] = 'node-teaser';
}
if (isset($variables['preview'])) {
$variables['classes_array'][] = 'node-preview';
}
// Clean up name so there are no underscores.
$variables['theme_hook_suggestions'][] = 'node__' . $node->type;
$variables['theme_hook_suggestions'][] = 'node__' . $node->nid;
}
/**
* @implements system__permission()
*/
function system__permission() {
$perms = array(
'bypass node access' => array(
'title' => drupal\t('Bypass content access control'),
'description' => drupal\t('View, edit and delete all content regardless of permission restrictions.'),
'restrict access' => TRUE,
),
'administer content types' => array(
'title' => drupal\t('Administer content types'),
'restrict access' => TRUE,
),
'administer nodes' => array(
'title' => drupal\t('Administer content'),
'restrict access' => TRUE,
),
'access content overview' => array(
'title' => drupal\t('Access the content overview page'),
),
'access content' => array(
'title' => drupal\t('View published content'),
),
'view own unpublished content' => array(
'title' => drupal\t('View own unpublished content'),
),
'view revisions' => array(
'title' => drupal\t('View content revisions'),
),
'revert revisions' => array(
'title' => drupal\t('Revert content revisions'),
),
'delete revisions' => array(
'title' => drupal\t('Delete content revisions'),
),
);
// Generate standard node permissions for all applicable node types.
foreach (permissions_get_configured_types() as $type) {
$perms += list_permissions($type);
}
return $perms;
}
/**
* Gather the rankings from the the hook_ranking implementations.
*
* @param $query
* A query object that has been extended with the Search DB Extender.
*/
function rankings(SelectQueryExtender $query) {
if ($ranking = drupal\invoke_all('node', 'ranking')) {
$tables = &$query->getTables();
foreach ($ranking as $rank => $values) {
if ($node_rank = drupal\variable_get('node_rank_' . $rank, 0)) {
// If the table defined in the ranking isn't already joined, then add it.
if (isset($values['join']) && !isset($tables[$values['join']['alias']])) {
$query->addJoin($values['join']['type'], $values['join']['table'], $values['join']['alias'], $values['join']['on']);
}
$arguments = isset($values['arguments']) ? $values['arguments'] : array();
$query->addScore($values['score'], $arguments, $node_rank);
}
}
}
}
/**
* @implements search__info()
*/
function search__info() {
return array(
'title' => 'Content',
'path' => 'node',
);
}
/**
* @implements search__access()
*/
function search__access() {
return user\access('access content');
}
/**
* @implements search__reset()
*/
function search__reset() {
db\update('search_dataset')
->fields(array('reindex' => REQUEST_TIME))
->condition('type', 'node')
->execute();
}
/**
* @implements search__status()
*/
function search__status() {
$total = db\query('SELECT COUNT(*) FROM {node}')->fetchField();
$remaining = db\query("SELECT COUNT(*) FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0")->fetchField();
return array('remaining' => $remaining, 'total' => $total);
}
/**
* @implements search__admin()
*/
function search__admin() {
// Output form for defining rank factor weights.
$form['content_ranking'] = array(
'#type' => 'fieldset',
'#title' => drupal\t('Content ranking'),
);
$form['content_ranking']['#theme'] = 'node_search_admin';
$form['content_ranking']['info'] = array(
'#value' => '' . drupal\t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . ''
);
// Note: reversed to reflect that higher number = higher ranking.
$options = drupal\map_assoc(range(0, 10));
foreach (drupal\invoke_all('ranking') as $var => $values) {
$form['content_ranking']['factors']['node_rank_' . $var] = array(
'#title' => $values['title'],
'#type' => 'select',
'#options' => $options,
'#default_value' => drupal\variable_get('node_rank_' . $var, 0),
);
}
return $form;
}
/**
* @implements search__execute()
*/
function search__execute($keys = NULL, $conditions = NULL) {
// Build matching conditions
$query = db\select('search_index', 'i', array('target' => 'slave'))->extend('SearchQuery')->extend('PagerDefault');
$query->join('node', 'n', 'n.nid = i.sid');
$query
->condition('n.status', 1)
->addTag('node_access')
->searchExpression($keys, 'node');
// Insert special keywords.
$query->setOption('type', 'n.type');
$query->setOption('language', 'n.language');
if ($query->setOption('term', 'ti.tid')) {
$query->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
}
// Only continue if the first pass query matches.
if (!$query->executeFirstPass()) {
return array();
}
// Add the ranking expressions.
rankings($query);
// Load results.
$find = $query
->limit(10)
->execute();
$results = array();
foreach ($find as $item) {
// Render the node.
$node = load($item->sid);
$build = view($node, 'search_result');
unset($build['#theme']);
$node->rendered = drupal\render($build);
// Fetch comments for snippet.
$node->rendered .= ' ' . drupal\invoke('comment', 'comment', 'node_update_index', $node);
$extra = drupal\invoke_all('node', 'search_result', $node);
$uri = drupal\entity_uri('node', $node);
$results[] = array(
'link' => drupal\url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE))),
'type' => drupal\check_plain(type_get_name($node)),
'title' => $node->title,
'user' => drupal\theme('system', 'username', array('account' => $node)),
'date' => $node->changed,
'node' => $node,
'extra' => $extra,
'score' => $item->calculated_score,
'snippet' => search\excerpt($keys, $node->rendered),
);
}
return $results;
}
/**
* @implements search__ranking()
*/
function search__ranking() {
// Create the ranking array and add the basic ranking options.
$ranking = array(
'relevance' => array(
'title' => drupal\t('Keyword relevance'),
// Average relevance values hover around 0.15
'score' => 'i.relevance',
),
'sticky' => array(
'title' => drupal\t('Content is sticky at top of lists'),
// The sticky flag is either 0 or 1, which is automatically normalized.
'score' => 'n.sticky',
),
'promote' => array(
'title' => drupal\t('Content is promoted to the front page'),
// The promote flag is either 0 or 1, which is automatically normalized.
'score' => 'n.promote',
),
);
// Add relevance based on creation or changed date.
if ($node_cron_last = drupal\variable_get('node_cron_last', 0)) {
$ranking['recent'] = array(
'title' => drupal\t('Recently posted'),
// Exponential decay with half-life of 6 months, starting at last indexed node
'score' => 'POW(2.0, (GREATEST(n.created, n.changed) - :node_cron_last) * 6.43e-8)',
'arguments' => array(':node_cron_last' => $node_cron_last),
);
}
return $ranking;
}
/**
* @implements user__cancel()
*/
function user__cancel($edit, $account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
// Unpublish nodes (current revisions).
drupal\module_load_include('inc', 'node', 'node.admin');
$nodes = db\select('node', 'n')
->fields('n', array('nid'))
->condition('uid', $account->uid)
->execute()
->fetchCol();
mass_update($nodes, array('status' => 0));
break;
case 'user_cancel_reassign':
// Anonymize nodes (current revisions).
drupal\module_load_include('inc', 'node', 'node.admin');
$nodes = db\select('node', 'n')
->fields('n', array('nid'))
->condition('uid', $account->uid)
->execute()
->fetchCol();
mass_update($nodes, array('uid' => 0));
// Anonymize old revisions.
db\update('node_revision')
->fields(array('uid' => 0))
->condition('uid', $account->uid)
->execute();
// Clean history.
db\delete('history')
->condition('uid', $account->uid)
->execute();
break;
}
}
/**
* @implements user__delete()
*/
function user__delete($account) {
// Delete nodes (current revisions).
// @todo Introduce mass_delete() or make mass_update() more flexible.
$nodes = db\select('node', 'n')
->fields('n', array('nid'))
->condition('uid', $account->uid)
->execute()
->fetchCol();
delete_multiple($nodes);
// Delete old revisions.
$revisions = db\query('SELECT vid FROM {node_revision} WHERE uid = :uid', array(':uid' => $account->uid))->fetchCol();
foreach ($revisions as $revision) {
revision_delete($revision);
}
// Clean history.
db\delete('history')
->condition('uid', $account->uid)
->execute();
}
/**
* Returns HTML for the content ranking part of the search settings admin page.
*
* @param $variables
* An associative array containing:
* - form: A render element representing the form.
*
* @ingroup themeable
*/
function node__theme__search_admin($variables) {
$form = $variables['form'];
$output = drupal\render($form['info']);
$header = array(t('Factor'), drupal\t('Weight'));
foreach (drupal\element_children($form['factors']) as $key) {
$row = array();
$row[] = $form['factors'][$key]['#title'];
$form['factors'][$key]['#title_display'] = 'invisible';
$row[] = drupal\render($form['factors'][$key]);
$rows[] = $row;
}
$output .= drupal\theme('table', array('header' => $header, 'rows' => $rows));
$output .= drupal\render_children($form);
return $output;
}
function revision_access($node, $op = 'view') {
$access = &drupal\static(__FUNCTION__, array());
if (!isset($access[$node->vid])) {
// To save additional calls to the database, return early if the user
// doesn't have the required permissions.
$map = array('view' => 'view revisions', 'update' => 'revert revisions', 'delete' => 'delete revisions');
if (isset($map[$op]) && (!user\access($map[$op]) && !user\access('administer nodes'))) {
$access[$node->vid] = FALSE;
return FALSE;
}
$node_current_revision = load($node->nid);
$is_current_revision = $node_current_revision->vid == $node->vid;
// There should be at least two revisions. If the vid of the given node
// and the vid of the current revision differs, then we already have two
// different revisions so there is no need for a separate database check.
// Also, if you try to revert to or delete the current revision, that's
// not good.
if ($is_current_revision && (db\query('SELECT COUNT(vid) FROM {node_revision} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField() == 1 || $op == 'update' || $op == 'delete')) {
$access[$node->vid] = FALSE;
}
elseif (user\access('administer nodes')) {
$access[$node->vid] = TRUE;
}
else {
// First check the access to the current revision and finally, if the
// node passed in is not the current revision then access to that, too.
$access[$node->vid] = node\access($op, $node_current_revision) && ($is_current_revision || node\access($op, $node));
}
}
return $access[$node->vid];
}
function add_access() {
$types = type_get_types();
foreach ($types as $type) {
if (hook($type->type, 'form') && node\access('create', $type->type)) {
return TRUE;
}
}
if (user\access('administer content types')) {
// There are no content types defined that the user has permission to create,
// but the user does have the permission to administer the content types, so
// grant them access to the page anyway.
return TRUE;
}
return FALSE;
}
/**
* @implements system__menu()
*/
function system__menu() {
$items['admin/content'] = array(
'title' => 'Content',
'description' => 'Find and manage content.',
'page callback' => 'drupal\get_form',
'page arguments' => array('drupal\node\admin_content'),
'access arguments' => array('access content overview'),
'weight' => -10,
'file' => 'node.admin.inc',
);
$items['admin/content/node'] = array(
'title' => 'Content',
'description' => "Administer content",
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/reports/status/rebuild'] = array(
'title' => 'Rebuild permissions',
'page callback' => 'drupal\get_form',
'page arguments' => array('\drupal\node\configure_rebuild_confirm'),
// Any user than can potentially trigger a access_needs_rebuild(TRUE)
// has to be allowed access to the 'node access rebuild' confirm form.
'access arguments' => array('access administration pages'),
'type' => MENU_CALLBACK,
'file' => 'node.admin.inc',
);
$items['admin/structure/types'] = array(
'title' => 'Content types',
'description' => 'Manage content types, including default status, front page promotion, comment settings, etc.',
'page callback' => 'drupal\node\overview_types',
'access arguments' => array('administer content types'),
'file' => 'content_types.inc',
);
$items['admin/structure/types/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/structure/types/add'] = array(
'title' => 'Add content type',
'page callback' => 'drupal\get_form',
'page arguments' => array('drupal\node\type_form'),
'access arguments' => array('administer content types'),
'type' => MENU_LOCAL_ACTION,
'file' => 'content_types.inc',
);
$items['admin/structure/types/manage/%node_type'] = array(
'title' => 'Edit content type',
'title callback' => 'drupal\node\type_page_title',
'title arguments' => array(4),
'page callback' => 'drupal\get_form',
'page arguments' => array('drupal\node\type_form', 4),
'access arguments' => array('administer content types'),
'file' => 'content_types.inc',
);
$items['admin/structure/types/manage/%node_type/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/types/manage/%node_type/delete'] = array(
'title' => 'Delete',
'page arguments' => array('drupal\node\type_delete_confirm', 4),
'access arguments' => array('administer content types'),
'file' => 'content_types.inc',
);
$items['node'] = array(
'page callback' => 'drupal\node\page_default',
'access arguments' => array('access content'),
// Required to make 'node/add' appear on the top-level of 'navigation' menu.
'menu_name' => '',
'type' => MENU_CALLBACK,
);
$items['node/add'] = array(
'title' => 'Add content',
'page callback' => 'drupal\node\add_page',
'access callback' => 'drupal\node\add_access',
'menu_name' => 'navigation',
'theme callback' => 'drupal\node\custom_theme',
'file' => 'node.pages.inc',
);
$items['rss.xml'] = array(
'title' => 'RSS feed',
'page callback' => 'drupal\node\feed',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
// @todo Remove this loop when we have a 'description callback' property.
// Reset internal static cache of types_build(), forces to rebuild the
// node type information.
drupal\static_reset('drupal\node\types_build');
foreach (type_get_types() as $type) {
$type_url_str = str_replace('_', '-', $type->type);
$items['node/add/' . $type_url_str] = array(
'title' => $type->name,
'title callback' => 'drupal\check_plain',
'page callback' => 'drupal\node\add',
'page arguments' => array($type->type),
'access callback' => 'drupal\node\access',
'access arguments' => array('create', $type->type),
'description' => $type->description,
'file' => 'node.pages.inc',
);
}
$items['node/%node'] = array(
'title callback' => 'drupal\node\page_title',
'title arguments' => array(1),
// The page callback also invokes drupal\set_title() in case
// the menu router's title is overridden by a menu link.
'page callback' => 'drupal\node\page_view',
'page arguments' => array(1),
'access callback' => 'drupal\node\access',
'access arguments' => array('view', 1),
);
$items['node/%node/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['node/%node/edit'] = array(
'title' => 'Edit',
'page callback' => 'drupal\node\page_edit',
'page arguments' => array(1),
'access callback' => 'drupal\node\access',
'access arguments' => array('update', 1),
'theme callback' => 'drupal\node\custom_theme',
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'node.pages.inc',
);
$items['node/%node/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal\get_form',
'page arguments' => array('drupal\node\delete_confirm', 1),
'access callback' => 'drupal\node\access',
'access arguments' => array('delete', 1),
'theme callback' => 'drupal\node\custom_theme',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'node.pages.inc',
);
$items['node/%node/revisions'] = array(
'title' => 'Revisions',
'page callback' => 'drupal\node\revision_overview',
'page arguments' => array(1),
'access callback' => 'drupal\node\revision_access',
'access arguments' => array(1),
'theme callback' => 'drupal\node\custom_theme',
'weight' => 2,
'type' => MENU_LOCAL_TASK,
'file' => 'node.pages.inc',
);
$items['node/%node/revisions/%/view'] = array(
'title' => 'Revisions',
'load arguments' => array(3),
'page callback' => 'drupal\node\show',
'page arguments' => array(1, TRUE),
'access callback' => 'drupal\node\revision_access',
'access arguments' => array(1),
);
$items['node/%node/revisions/%/revert'] = array(
'title' => 'Revert to earlier revision',
'load arguments' => array(3),
'page callback' => 'drupal\get_form',
'page arguments' => array('drupal\node\revision_revert_confirm', 1),
'access callback' => 'drupal\node\revision_access',
'access arguments' => array(1, 'update'),
'theme callback' => 'drupal\node\custom_theme',
'file' => 'node.pages.inc',
);
$items['node/%node/revisions/%/delete'] = array(
'title' => 'Delete earlier revision',
'load arguments' => array(3),
'page callback' => 'drupal\get_form',
'page arguments' => array('drupal\node\revision_delete_confirm', 1),
'access callback' => 'drupal\node\revision_access',
'access arguments' => array(1, 'delete'),
'theme callback' => 'drupal\node\custom_theme',
'file' => 'node.pages.inc',
);
return $items;
}
/**
* @implements system__menu_local_tasks_alter()
*/
function system__menu_local_tasks_alter(&$data, $router_item, $root_path) {
// Add action link to 'node/add' on 'admin/content' page.
if ($root_path == 'admin/content') {
$item = drupal\menu_get_item('node/add');
if ($item['access']) {
$data['actions']['output'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
/**
* Title callback for a node type.
*/
function type_page_title($type) {
return $type->name;
}
/**
* Title callback.
*/
function page_title($node) {
return $node->title;
}
/**
* Theme callback for creating and editing nodes.
*/
function custom_theme() {
// Use the administration theme if the site is configured to use it for
// nodes.
if (drupal\variable_get('node_admin_theme')) {
return drupal\variable_get('admin_theme');
}
}
function last_changed($nid) {
return db\query('SELECT changed FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetch()->changed;
}
/**
* Return a list of all the existing revision numbers.
*/
function revision_list($node) {
$revisions = array();
$result = db\query('SELECT r.vid, r.title, r.log, r.uid, n.vid AS current_vid, r.timestamp, u.name FROM {node_revision} r LEFT JOIN {node} n ON n.vid = r.vid INNER JOIN {users} u ON u.uid = r.uid WHERE r.nid = :nid ORDER BY r.vid DESC', array(':nid' => $node->nid));
foreach ($result as $revision) {
$revisions[$revision->vid] = $revision;
}
return $revisions;
}
/**
* @implements block__info()
*/
function block__info() {
$blocks['syndicate']['info'] = drupal\t('Syndicate');
// Not worth caching.
$blocks['syndicate']['cache'] = DRUPAL_NO_CACHE;
$blocks['recent']['info'] = drupal\t('Recent content');
return $blocks;
}
/**
* @implements block__view()
*/
function block__view($delta = '') {
$block = array();
switch ($delta) {
case 'syndicate':
$block['subject'] = drupal\t('Syndicate');
$block['content'] = drupal\theme('feed_icon', array('url' => 'rss.xml', 'title' => drupal\t('Syndicate')));
break;
case 'recent':
if (user\access('access content')) {
$block['subject'] = drupal\t('Recent content');
if ($nodes = get_recent(drupal\variable_get('node_recent_block_count', 10))) {
$block['content'] = drupal\theme('node_recent_block', array(
'nodes' => $nodes,
));
} else {
$block['content'] = drupal\t("No content available.");
}
}
break;
}
return $block;
}
/**
* @implements block__configure()
*/
function block__configure($delta = '') {
$form = array();
if ($delta == 'recent') {
$form['node_recent_block_count'] = array(
'#type' => 'select',
'#title' => drupal\t('Number of recent content items to display'),
'#default_value' => drupal\variable_get('node_recent_block_count', 10),
'#options' => drupal\map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
);
}
return $form;
}
/**
* @implements block__save()
*/
function block__save($delta = '', $edit = array()) {
if ($delta == 'recent') {
drupal\variable_set('node_recent_block_count', $edit['node_recent_block_count']);
}
}
/**
* Find the most recent nodes that are available to the current user.
*
* @public
*
* @param $number
* (optional) The maximum number of nodes to find. Defaults to 10.
*
* @return
* An array of partial node objects or an empty array if there are no recent
* nodes visible to the current user.
*/
function get_recent($number = 10) {
$query = db\select('node', 'n');
if (!user\access('bypass node access')) {
// If the user is able to view their own unpublished nodes, allow them
// to see these in addition to published nodes. Check that they actually
// have some unpublished nodes to view before adding the condition.
if (user\access('view own unpublished content') && $own_unpublished = db\query('SELECT nid FROM {node} WHERE uid = :uid AND status = :status', array(':uid' => $GLOBALS['user']->uid, ':status' => NODE_NOT_PUBLISHED))->fetchCol()) {
$query->condition(db\or()
->condition('n.status', NODE_PUBLISHED)
->condition('n.nid', $own_unpublished, 'IN')
);
}
else {
// If not, restrict the query to published nodes.
$query->condition('n.status', NODE_PUBLISHED);
}
}
$nids = $query
->fields('n', array('nid'))
->orderBy('changed', 'DESC')
->range(0, $number)
->addTag('node_access')
->execute()
->fetchCol();
$nodes = load_multiple($nids);
return $nodes ? $nodes : array();
}
/**
* Returns HTML for a list of recent content.
*
* @param $variables
* An associative array containing:
* - nodes: An array of recent node objects.
*
* @ingroup themeable
*/
function node__theme__recent_block($variables) {
$rows = array();
$output = '';
$l_options = array('query' => drupal\get_destination());
foreach ($variables['nodes'] as $node) {
$row = array();
$row[] = array(
'data' => drupal\theme('node_recent_content', array('node' => $node)),
'class' => 'title-author',
);
$row[] = array(
'data' => node\access('update', $node) ? l(t('edit'), 'node/' . $node->nid . '/edit', $l_options) : '',
'class' => 'edit',
);
$row[] = array(
'data' => node\access('delete', $node) ? l(t('delete'), 'node/' . $node->nid . '/delete', $l_options) : '',
'class' => 'delete',
);
$rows[] = $row;
}
if ($rows) {
$output = drupal\theme('table', array('rows' => $rows));
if (user\access('access content overview')) {
$output .= drupal\theme('more_link', array('url' => 'admin/content', 'title' => drupal\t('Show more content')));
}
}
return $output;
}
/**
* Returns HTML for a recent node to be displayed in the recent content block.
*
* @param $variables
* An associative array containing:
* - node: A node object.
*
* @ingroup themeable
*/
function node__theme__recent_content($variables) {
$node = $variables['node'];
$output = '' . drupal\t('No front page content has been created yet.') . '
'; $default_links = array(); if (add_access()) { $default_links[] = l(t('Add new content'), 'node/add'); } if (!empty($default_links)) { $default_message .= drupal\theme('item_list', array('items' => $default_links)); } $build['default_message'] = array( '#markup' => $default_message, '#prefix' => '