<?php
use Drupal\Core\Language\Language;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Template\Attribute;
use Drupal\entity\Plugin\Core\Entity\EntityDisplay;
use Drupal\file\Plugin\Core\Entity\File;
use Drupal\user\UserInterface;
const NODE_NOT_PUBLISHED = 0;
const NODE_PUBLISHED = 1;
const NODE_NOT_PROMOTED = 0;
const NODE_PROMOTED = 1;
const NODE_NOT_STICKY = 0;
const NODE_STICKY = 1;
const NODE_ACCESS_ALLOW = TRUE;
const NODE_ACCESS_DENY = FALSE;
const NODE_ACCESS_IGNORE = NULL;
function node_rebuild() {
node_types_rebuild();
}
function node_help($path, $arg) {
if ($path != 'admin/reports/status/rebuild' && $path != 'batch' && strpos($path, '#') === FALSE && user_access('access administration pages') && node_access_needs_rebuild()) {
if ($path == 'admin/reports/status') {
$message = t('The content access permissions need to be rebuilt.');
}
else {
$message = t('The content access permissions need to be rebuilt. <a href="@node_access_rebuild">Rebuild permissions</a>.', array(
'@node_access_rebuild' => url('admin/reports/status/rebuild'),
));
}
drupal_set_message($message, 'error');
}
switch ($path) {
case 'admin/help#node':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . 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 <a href="@field">Field module</a>). For more information, see the online handbook entry for <a href="@node">Node module</a>.', array(
'@node' => 'http://drupal.org/documentation/modules/node',
'@field' => url('admin/help/field'),
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Creating content') . '</dt>';
$output .= '<dd>' . t('When new content is created, the Node module records basic information about the content, including the author, date of creation, and the <a href="@content-type">Content type</a>. It also manages the <em>publishing options</em>, which define whether or not the content is published, promoted to the front page of the site, and/or sticky at the top of content lists. Default settings can be configured for each <a href="@content-type">type of content</a> on your site.', array(
'@content-type' => url('admin/structure/types'),
)) . '</dd>';
$output .= '<dt>' . t('Creating custom content types') . '</dt>';
$output .= '<dd>' . t('The Node module gives users with the <em>Administer content types</em> permission the ability to <a href="@content-new">create new content types</a> in addition to the default ones already configured. Creating custom content types allows you the flexibility to add <a href="@field">fields</a> and configure default settings that suit the differing needs of various site content.', array(
'@content-new' => url('admin/structure/types/add'),
'@field' => url('admin/help/field'),
)) . '</dd>';
$output .= '<dt>' . t('Administering content') . '</dt>';
$output .= '<dd>' . t('The <a href="@content">Content administration page</a> allows you to review and bulk manage your site content.', array(
'@content' => url('admin/content'),
)) . '</dd>';
$output .= '<dt>' . t('Creating revisions') . '</dt>';
$output .= '<dd>' . t('The Node module also enables you to create multiple versions of any content, and revert to older versions using the <em>Revision information</em> settings.') . '</dd>';
$output .= '<dt>' . t('User permissions') . '</dt>';
$output .= '<dd>' . t('The Node module makes a number of permissions available for each content type, which can be set by role on the <a href="@permissions">permissions page</a>.', array(
'@permissions' => url('admin/people/permissions', array(
'fragment' => 'module-node',
)),
)) . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/structure/types/add':
return '<p>' . t('Individual content types can have different fields, behaviors, and permissions assigned to them.') . '</p>';
case 'admin/structure/types/manage/%/display':
return '<p>' . t('Content items can be displayed using different view modes: Teaser, Full content, Print, RSS, etc. <em>Teaser</em> is a short format that is typically used in lists of multiple content items. <em>Full content</em> is typically used when the content is displayed on its own page.') . '</p>' . '<p>' . 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' => node_type_get_label($arg[4]),
)) . '</p>';
case 'node/%/revisions':
return '<p>' . t('Revisions allow you to track differences between multiple versions of your content, and revert back to older versions.') . '</p>';
case 'node/%/edit':
$node = node_load($arg[1]);
$type = node_type_load($node->type);
return !empty($type->help) ? '<p>' . filter_xss_admin($type->help) . '</p>' : '';
}
if ($arg[0] == 'node' && $arg[1] == 'add' && $arg[2]) {
$type = node_type_load($arg[2]);
return !empty($type->help) ? '<p>' . filter_xss_admin($type->help) . '</p>' : '';
}
}
function node_theme() {
return array(
'node' => array(
'render element' => 'elements',
'template' => 'node',
),
'node_search_admin' => array(
'render element' => 'form',
),
'node_add_list' => array(
'variables' => array(
'content' => NULL,
),
'file' => 'node.pages.inc',
),
'node_preview' => array(
'variables' => array(
'node' => NULL,
),
'file' => 'node.pages.inc',
),
'node_admin_overview' => array(
'variables' => array(
'name' => NULL,
'type' => NULL,
),
'file' => 'content_types.inc',
),
'node_recent_block' => array(
'variables' => array(
'nodes' => NULL,
),
),
'node_recent_content' => array(
'variables' => array(
'node' => NULL,
),
),
'node_edit_form' => array(
'render element' => 'form',
'template' => 'node-edit-form',
),
);
}
function node_entity_bundle_info() {
$bundles = array();
node_type_cache_reset();
foreach (node_type_get_names() as $type => $name) {
$bundles['node'][$type]['label'] = $name;
}
return $bundles;
}
function node_entity_display_alter(EntityDisplay $display, $context) {
if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') {
foreach ($display
->getComponents() as $name => $options) {
if (isset($options['label'])) {
$options['label'] = 'hidden';
$display
->setComponent($name, $options);
}
}
}
}
function node_uri(EntityInterface $node) {
return array(
'path' => 'node/' . $node->nid->value,
);
}
function node_admin_paths() {
if (variable_get('node_admin_theme')) {
$paths = array(
'node/*/edit' => TRUE,
'node/*/delete' => TRUE,
'node/*/revisions' => TRUE,
'node/*/revisions/*/revert' => TRUE,
'node/*/revisions/*/delete' => TRUE,
'node/*/translations' => TRUE,
'node/*/translations/*' => TRUE,
'node/add' => TRUE,
'node/add/*' => TRUE,
);
return $paths;
}
}
function node_title_list($result, $title = NULL) {
$items = array();
$num_rows = FALSE;
foreach ($result as $node) {
$items[] = l($node->title, 'node/' . $node->nid, !empty($node->comment_count) ? array(
'attributes' => array(
'title' => 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;
}
function node_mark($nid, $timestamp) {
global $user;
$cache =& drupal_static(__FUNCTION__, array());
if (!$user->uid || !module_exists('history')) {
return MARK_READ;
}
if (!isset($cache[$nid])) {
$cache[$nid] = history_read($nid);
}
if ($cache[$nid] == 0 && $timestamp > HISTORY_READ_LIMIT) {
return MARK_NEW;
}
elseif ($timestamp > $cache[$nid] && $timestamp > HISTORY_READ_LIMIT) {
return MARK_UPDATED;
}
return MARK_READ;
}
function node_type_get_types() {
return _node_types_build()->types;
}
function node_type_get_base($type) {
$types = _node_types_build()->types;
return isset($types[$type]) && isset($types[$type]->base) ? $types[$type]->base : FALSE;
}
function node_type_get_names() {
return _node_types_build()->names;
}
function node_type_get_label($name) {
$types = _node_types_build()->names;
return isset($types[$name]) ? $types[$name] : FALSE;
}
function node_get_type_label(EntityInterface $node) {
$types = _node_types_build()->names;
return isset($types[$node->type]) ? $types[$node->type] : FALSE;
}
function node_type_get_clean_name($node_type) {
return check_plain($node_type->name);
}
function node_type_get_description($node_type) {
return $node_type->description;
}
function node_types_rebuild() {
_node_types_build(TRUE);
}
function node_type_load($name) {
$types = _node_types_build()->types;
return isset($types[$name]) ? $types[$name] : FALSE;
}
function node_type_save($info) {
$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 = node_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) {
entity_invoke_bundle_hook('rename', 'node', $type->old_type, $type->type);
}
module_invoke_all('node_type_update', $type);
$status = SAVED_UPDATED;
}
else {
$fields['orig_type'] = (string) $type->orig_type;
db_insert('node_type')
->fields($fields)
->execute();
entity_invoke_bundle_hook('create', 'node', $type->type);
module_invoke_all('node_type_insert', $type);
$status = SAVED_NEW;
}
node_type_cache_reset();
return $status;
}
function node_add_body_field($type, $label = 'Body') {
$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',
),
);
$field = field_create_field($field);
}
if (empty($instance)) {
$instance = array(
'field_name' => 'body',
'entity_type' => 'node',
'bundle' => $type->type,
'label' => $label,
'settings' => array(
'display_summary' => TRUE,
),
);
$instance = field_create_instance($instance);
entity_get_form_display('node', $type->type, 'default')
->setComponent($field['field_name'], array(
'type' => 'text_textarea_with_summary',
))
->save();
entity_get_display('node', $type->type, 'default')
->setComponent($field['field_name'], array(
'label' => 'hidden',
'type' => 'text_default',
))
->save();
entity_get_display('node', $type->type, 'teaser')
->setComponent($field['field_name'], array(
'label' => 'hidden',
'type' => 'text_summary_or_trimmed',
))
->save();
}
return $instance;
}
function node_field_extra_fields() {
$extra = array();
$module_language_enabled = module_exists('language');
$description = t('Node module element');
foreach (node_type_get_types() as $bundle) {
if ($bundle->has_title) {
$extra['node'][$bundle->type]['form']['title'] = array(
'label' => $bundle->title_label,
'description' => $description,
'weight' => -5,
);
}
if ($module_language_enabled) {
$configuration = language_get_default_configuration('node', $bundle->type);
if ($configuration['language_show']) {
$extra['node'][$bundle->type]['form']['language'] = array(
'label' => t('Language'),
'description' => $description,
'weight' => 0,
);
}
}
$extra['node'][$bundle->type]['display']['language'] = array(
'label' => t('Language'),
'description' => $description,
'weight' => 0,
'visible' => FALSE,
);
}
return $extra;
}
function node_type_delete($name) {
$type = node_type_load($name);
db_delete('node_type')
->condition('type', $name)
->execute();
entity_invoke_bundle_hook('delete', 'node', $name);
module_invoke_all('node_type_delete', $type);
node_type_cache_reset();
}
function node_type_update_nodes($old_type, $type) {
return db_update('node')
->fields(array(
'type' => $type,
))
->condition('type', $old_type)
->execute();
}
function _node_types_build($rebuild = FALSE) {
$cid = 'node_types:' . language(Language::TYPE_INTERFACE)->langcode;
if (!$rebuild) {
$_node_types =& drupal_static(__FUNCTION__);
if (isset($_node_types)) {
return $_node_types;
}
if ($cache = cache()
->get($cid)) {
$_node_types = $cache->data;
return $_node_types;
}
}
$_node_types = (object) array(
'types' => array(),
'names' => array(),
);
foreach (module_implements('node_info') as $module) {
$info_array = module_invoke($module, 'node_info');
foreach ($info_array as $type => $info) {
$info['type'] = $type;
$_node_types->types[$type] = node_type_set_defaults($info);
$_node_types->types[$type]->module = $module;
$_node_types->names[$type] = $info['name'];
}
}
$query = db_select('node_type', 'nt')
->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;
$disabled = $type_object->disabled;
if (isset($type_object->base) && $type_object->base != 'node_content' && empty($_node_types->types[$type_db])) {
$type_object->disabled = TRUE;
}
if (isset($_node_types->types[$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);
cache()
->set($cid, $_node_types, CacheBackendInterface::CACHE_PERMANENT, array(
'node_types' => TRUE,
));
return $_node_types;
}
function node_type_cache_reset() {
cache()
->deleteTags(array(
'node_types' => TRUE,
));
drupal_static_reset('_node_types_build');
}
function node_type_set_defaults($info = array()) {
$info = (array) $info;
$new_type = $info + array(
'type' => '',
'name' => '',
'base' => '',
'description' => '',
'help' => '',
'custom' => 0,
'modified' => 0,
'locked' => 1,
'disabled' => 0,
'is_new' => 1,
'has_title' => 1,
'title_label' => 'Title',
);
$new_type = (object) $new_type;
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;
}
function node_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',
),
),
),
);
}
function node_hook($type, $hook) {
$base = node_type_get_base($type);
return module_hook($base, $hook) ? $base . '_' . $hook : FALSE;
}
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);
}
}
function node_load_multiple(array $nids = NULL, $reset = FALSE) {
$entities = entity_load_multiple('node', $nids, $reset);
foreach ($entities as $id => $entity) {
$entities[$id] = $entity
->getBCEntity();
}
return $entities;
}
function node_load($nid = NULL, $reset = FALSE) {
$entity = entity_load('node', $nid, $reset);
return $entity ? $entity
->getBCEntity() : FALSE;
}
function node_revision_load($vid = NULL) {
return entity_revision_load('node', $vid);
}
function node_submit(EntityInterface $node) {
global $user;
if (isset($node->name)) {
if ($account = user_load_by_name($node->name)) {
$node->uid = $account->uid;
}
else {
$node->uid = 0;
}
}
if ($node
->isNewRevision()) {
$node->revision_uid = $user->uid;
$node->revision_timestamp = REQUEST_TIME;
}
$node->created = !empty($node->date) && $node->date instanceof DrupalDateTime ? $node->date
->getTimestamp() : REQUEST_TIME;
$node->validated = TRUE;
return $node;
}
function node_revision_delete($revision_id) {
entity_revision_delete('node', $revision_id);
}
function node_show(EntityInterface $node, $message = FALSE) {
if ($message) {
drupal_set_title(t('Revision of %title from %date', array(
'%title' => $node
->label(),
'%date' => format_date($node->revision_timestamp),
)), PASS_THROUGH);
}
$nodes = array(
'nodes' => node_view_multiple(array(
$node->nid => $node,
), 'full'),
);
if (module_exists('history')) {
history_write($node->nid);
}
return $nodes;
}
function node_is_page(EntityInterface $node) {
$page_node = menu_get_object();
return !empty($page_node) ? $page_node->nid == $node->nid : FALSE;
}
function node_preprocess_block(&$variables) {
if ($variables['configuration']['module'] == 'node') {
switch ($variables['elements']['#plugin_id']) {
case 'node_syndicate_block':
$variables['attributes']['role'] = 'complementary';
break;
case 'node_recent_block':
$variables['attributes']['role'] = 'navigation';
break;
}
}
}
function template_preprocess_node(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['teaser'] = $variables['view_mode'] == 'teaser';
$variables['node'] = $variables['elements']['#node'];
$node = $variables['node'];
$variables['date'] = format_date($node->created);
$username = array(
'#theme' => 'username',
'#account' => $node,
'#link_options' => array(
'attributes' => array(
'rel' => 'author',
),
),
);
$variables['name'] = drupal_render($username);
$uri = $node
->uri();
$variables['node_url'] = url($uri['path'], $uri['options']);
$variables['label'] = check_plain($node
->label());
$variables['page'] = $variables['view_mode'] == 'full' && node_is_page($node);
$variables += array(
'content' => array(),
);
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
field_attach_preprocess($node, $variables['content'], $variables);
if (variable_get('node_submitted_' . $node->type, TRUE)) {
$variables['display_submitted'] = TRUE;
$variables['submitted'] = t('Submitted by !username on !datetime', array(
'!username' => $variables['name'],
'!datetime' => $variables['date'],
));
if (theme_get_setting('features.node_user_picture')) {
$variables['user_picture'] = user_view($node->account, 'compact');
}
else {
$variables['user_picture'] = array();
}
}
else {
$variables['display_submitted'] = FALSE;
$variables['submitted'] = '';
$variables['user_picture'] = '';
}
$variables['attributes']['role'] = 'article';
$variables['attributes']['class'][] = 'node';
$variables['attributes']['class'][] = drupal_html_class('node-' . $node->type);
if ($node->promote) {
$variables['attributes']['class'][] = 'promoted';
}
if ($node->sticky) {
$variables['attributes']['class'][] = 'sticky';
}
if (!$node->status) {
$variables['attributes']['class'][] = 'unpublished';
}
if ($variables['view_mode']) {
$variables['attributes']['class'][] = drupal_html_class('view-mode-' . $variables['view_mode']);
}
if (isset($variables['preview'])) {
$variables['attributes']['class'][] = 'preview';
}
$variables['theme_hook_suggestions'][] = 'node__' . $node->type;
$variables['theme_hook_suggestions'][] = 'node__' . $node->nid;
$variables['content_attributes']['class'][] = 'content';
}
function node_permission() {
$perms = array(
'bypass node access' => array(
'title' => t('Bypass content access control'),
'description' => t('View, edit and delete all content regardless of permission restrictions.'),
'restrict access' => TRUE,
),
'administer content types' => array(
'title' => t('Administer content types'),
'restrict access' => TRUE,
),
'administer nodes' => array(
'title' => t('Administer content'),
'restrict access' => TRUE,
),
'access content overview' => array(
'title' => t('Access the Content overview page'),
'description' => user_access('access content overview') ? t('Get an overview of <a href="@url">all content</a>.', array(
'@url' => url('admin/content'),
)) : t('Get an overview of all content.'),
),
'access content' => array(
'title' => t('View published content'),
),
'view own unpublished content' => array(
'title' => t('View own unpublished content'),
),
'view all revisions' => array(
'title' => t('View all revisions'),
),
'revert all revisions' => array(
'title' => t('Revert all revisions'),
'description' => t('Role requires permission <em>view revisions</em> and <em>edit rights</em> for nodes in question, or <em>administer nodes</em>.'),
),
'delete all revisions' => array(
'title' => t('Delete all revisions'),
'description' => t('Role requires permission to <em>view revisions</em> and <em>delete rights</em> for nodes in question, or <em>administer nodes</em>.'),
),
);
foreach (node_permissions_get_configured_types() as $name => $type) {
$perms += node_list_permissions($type);
}
return $perms;
}
function _node_rankings(SelectExtender $query) {
if ($ranking = module_invoke_all('ranking')) {
$tables =& $query
->getTables();
foreach ($ranking as $rank => $values) {
if ($node_rank = variable_get('node_rank_' . $rank, 0)) {
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);
}
}
}
}
function node_search_info() {
return array(
'title' => 'Content',
'path' => 'node',
);
}
function node_search_access() {
return user_access('access content');
}
function node_search_reset() {
db_update('search_dataset')
->fields(array(
'reindex' => REQUEST_TIME,
))
->condition('type', 'node')
->execute();
}
function node_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,
);
}
function node_search_admin() {
$form['content_ranking'] = array(
'#type' => 'details',
'#title' => t('Content ranking'),
);
$form['content_ranking']['#theme'] = 'node_search_admin';
$form['content_ranking']['info'] = array(
'#value' => '<em>' . 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.') . '</em>',
);
$options = drupal_map_assoc(range(0, 10));
foreach (module_invoke_all('ranking') as $var => $values) {
$form['content_ranking']['factors']['node_rank_' . $var] = array(
'#title' => $values['title'],
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('node_rank_' . $var, 0),
);
}
return $form;
}
function node_search_execute($keys = NULL, $conditions = NULL) {
$query = db_select('search_index', 'i', array(
'target' => 'slave',
))
->extend('Drupal\\search\\SearchQuery')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$query
->join('node_field_data', 'n', 'n.nid = i.sid');
$query
->condition('n.status', 1)
->addTag('node_access')
->searchExpression($keys, 'node');
$query
->setOption('type', 'n.type');
$query
->setOption('langcode', 'n.langcode');
if ($query
->setOption('term', 'ti.tid')) {
$query
->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
}
if (!$query
->executeFirstPass()) {
return array();
}
_node_rankings($query);
$find = $query
->fields('i', array(
'langcode',
))
->limit(10)
->execute();
$results = array();
foreach ($find as $item) {
$node = node_load($item->sid);
$build = node_view($node, 'search_result', $item->langcode);
unset($build['#theme']);
$node->rendered = drupal_render($build);
$node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node, $item->langcode);
$extra = module_invoke_all('node_search_result', $node, $item->langcode);
$language = language_load($item->langcode);
$uri = $node
->uri();
$username = array(
'#theme' => 'username',
'#account' => $node,
);
$results[] = array(
'link' => url($uri['path'], array_merge($uri['options'], array(
'absolute' => TRUE,
'language' => $language,
))),
'type' => check_plain(node_get_type_label($node)),
'title' => $node
->label($item->langcode),
'user' => drupal_render($username),
'date' => $node->changed,
'node' => $node,
'extra' => $extra,
'score' => $item->calculated_score,
'snippet' => search_excerpt($keys, $node->rendered, $item->langcode),
'langcode' => $node->langcode,
);
}
return $results;
}
function node_ranking() {
$ranking = array(
'relevance' => array(
'title' => t('Keyword relevance'),
'score' => 'i.relevance',
),
'sticky' => array(
'title' => t('Content is sticky at top of lists'),
'score' => 'n.sticky',
),
'promote' => array(
'title' => t('Content is promoted to the front page'),
'score' => 'n.promote',
),
);
if ($node_cron_last = Drupal::state()
->get('node.cron_last')) {
$ranking['recent'] = array(
'title' => t('Recently posted'),
'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;
}
function node_user_cancel($edit, $account, $method) {
switch ($method) {
case 'user_cancel_block_unpublish':
module_load_include('inc', 'node', 'node.admin');
$nodes = db_select('node_field_data', 'n')
->distinct()
->fields('n', array(
'nid',
))
->condition('uid', $account->uid)
->execute()
->fetchCol();
node_mass_update($nodes, array(
'status' => 0,
), NULL, TRUE);
break;
case 'user_cancel_reassign':
module_load_include('inc', 'node', 'node.admin');
$nodes = db_select('node_field_data', 'n')
->distinct()
->fields('n', array(
'nid',
))
->condition('uid', $account->uid)
->execute()
->fetchCol();
node_mass_update($nodes, array(
'uid' => 0,
), NULL, TRUE);
db_update('node_field_revision')
->fields(array(
'uid' => 0,
))
->condition('uid', $account->uid)
->execute();
break;
}
}
function node_user_predelete($account) {
$nodes = db_select('node_field_data', 'n')
->distinct()
->fields('n', array(
'nid',
))
->condition('uid', $account->uid)
->execute()
->fetchCol();
entity_delete_multiple('node', $nodes);
$revisions = db_query('SELECT DISTINCT vid FROM {node_field_revision} WHERE uid = :uid', array(
':uid' => $account->uid,
))
->fetchCol();
foreach ($revisions as $revision) {
node_revision_delete($revision);
}
}
function theme_node_search_admin($variables) {
$form = $variables['form'];
$output = drupal_render($form['info']);
$header = array(
t('Factor'),
t('Weight'),
);
foreach (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;
}
$table = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$output .= drupal_render($table);
$output .= drupal_render_children($form);
return $output;
}
function _node_revision_access(EntityInterface $node, $op = 'view', $account = NULL, $langcode = NULL) {
$access =& drupal_static(__FUNCTION__, array());
$map = array(
'view' => 'view all revisions',
'update' => 'revert all revisions',
'delete' => 'delete all revisions',
);
$type_map = array(
'view' => "view {$node->type} revisions",
'update' => "revert {$node->type} revisions",
'delete' => "delete {$node->type} revisions",
);
if (!$node || !isset($map[$op]) || !isset($type_map[$op])) {
return FALSE;
}
if (!isset($account)) {
$account = $GLOBALS['user'];
}
if (empty($langcode)) {
$langcode = $node->langcode;
}
$cid = $node->vid . ':' . $langcode . ':' . $account->uid . ':' . $op;
if (!isset($access[$cid])) {
if (!user_access($map[$op], $account) && !user_access($type_map[$op], $account) && !user_access('administer nodes', $account)) {
return $access[$cid] = FALSE;
}
if ($node
->isDefaultRevision() && (db_query('SELECT COUNT(*) FROM {node_field_revision} WHERE nid = :nid AND default_langcode = 1', array(
':nid' => $node->nid,
))
->fetchField() == 1 || $op == 'update' || $op == 'delete')) {
$access[$cid] = FALSE;
}
elseif (user_access('administer nodes', $account)) {
$access[$cid] = TRUE;
}
else {
$access[$cid] = node_access($op, node_load($node->nid), $account, $langcode) && ($node
->isDefaultRevision() || node_access($op, $node, $account, $langcode));
}
}
return $access[$cid];
}
function _node_add_access() {
$types = node_type_get_types();
foreach ($types as $type) {
if (node_hook($type->type, 'form') && node_access('create', $type->type)) {
return TRUE;
}
}
if (user_access('administer content types')) {
return TRUE;
}
return FALSE;
}
function node_menu() {
$items['admin/content'] = array(
'title' => 'Content',
'description' => 'Find and manage content.',
'page callback' => 'node_admin_nodes',
'access arguments' => array(
'access content overview',
),
'weight' => -10,
'file' => 'node.admin.inc',
);
$items['admin/content/node'] = array(
'title' => 'Content',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/reports/status/rebuild'] = array(
'title' => 'Rebuild permissions',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_configure_rebuild_confirm',
),
'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' => '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,
);
$items['admin/structure/types/add'] = array(
'title' => 'Add content type',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'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' => 'node_type_page_title',
'title arguments' => array(
4,
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'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(
'node_type_delete_confirm',
4,
),
'access arguments' => array(
'administer content types',
),
'file' => 'content_types.inc',
);
$items['node/add'] = array(
'title' => 'Add content',
'page callback' => 'node_add_page',
'access callback' => '_node_add_access',
'file' => 'node.pages.inc',
);
$items['node/add/%node_type'] = array(
'title callback' => 'node_type_get_clean_name',
'title arguments' => array(
2,
),
'page callback' => 'node_add',
'page arguments' => array(
2,
),
'access callback' => 'node_access',
'access arguments' => array(
'create',
2,
),
'description callback' => 'node_type_get_description',
'description arguments' => array(
2,
),
'file' => 'node.pages.inc',
);
$items['node/%node'] = array(
'title callback' => 'node_page_title',
'title arguments' => array(
1,
),
'page callback' => 'node_page_view',
'page arguments' => array(
1,
),
'access callback' => 'node_access',
'access arguments' => array(
'view',
1,
),
);
$items['node/%node/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['node/%node/edit'] = array(
'title' => 'Edit',
'route_name' => 'node_page_edit',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
);
$items['node/%node/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_delete_confirm',
1,
),
'access callback' => 'node_access',
'access arguments' => array(
'delete',
1,
),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'node.pages.inc',
);
$items['node/%node/revisions'] = array(
'title' => 'Revisions',
'page callback' => 'node_revision_overview',
'page arguments' => array(
1,
),
'access callback' => '_node_revision_access',
'access arguments' => array(
1,
),
'weight' => 20,
'type' => MENU_LOCAL_TASK,
'file' => 'node.pages.inc',
);
$items['node/%node/revisions/%node_revision/view'] = array(
'title' => 'Revisions',
'page callback' => 'node_show',
'page arguments' => array(
3,
TRUE,
),
'access callback' => '_node_revision_access',
'access arguments' => array(
3,
),
);
$items['node/%node/revisions/%node_revision/revert'] = array(
'title' => 'Revert to earlier revision',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_revision_revert_confirm',
3,
),
'access callback' => '_node_revision_access',
'access arguments' => array(
3,
'update',
),
'file' => 'node.pages.inc',
);
$items['node/%node/revisions/%node_revision/delete'] = array(
'title' => 'Delete earlier revision',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'node_revision_delete_confirm',
3,
),
'access callback' => '_node_revision_access',
'access arguments' => array(
3,
'delete',
),
'file' => 'node.pages.inc',
);
return $items;
}
function node_menu_local_tasks(&$data, $router_item, $root_path) {
if ($root_path == 'admin/content') {
$item = menu_get_item('node/add');
if ($item['access']) {
$data['actions'][] = array(
'#theme' => 'menu_local_action',
'#link' => $item,
);
}
}
}
function node_type_page_title($type) {
return $type->name;
}
function node_page_title(EntityInterface $node) {
return $node
->label();
}
function node_last_changed($nid, $langcode = NULL) {
$language_clause = isset($langcode) ? 'langcode = :langcode' : 'default_langcode = 1';
$result = db_query('SELECT changed FROM {node_field_data} WHERE nid = :nid AND ' . $language_clause, array(
':nid' => $nid,
':langcode' => $langcode,
))
->fetch();
return is_object($result) ? $result->changed : FALSE;
}
function node_revision_list(EntityInterface $node) {
$revisions = array();
$result = db_query('SELECT nfr.vid, nfr.title, nfr.log, nfr.revision_uid AS uid, n.vid AS current_vid, nfr.revision_timestamp, u.name FROM {node_field_revision} nfr LEFT JOIN {node} n ON n.vid = nfr.vid INNER JOIN {users} u ON u.uid = nfr.revision_uid WHERE nfr.nid = :nid AND nfr.default_langcode = 1 ORDER BY nfr.vid DESC', array(
':nid' => $node->nid,
));
foreach ($result as $revision) {
$revisions[$revision->vid] = $revision;
}
return $revisions;
}
function node_get_recent($number = 10) {
$query = db_select('node_field_data', 'n');
if (!user_access('bypass node access')) {
if (user_access('view own unpublished content') && ($own_unpublished = db_query('SELECT DISTINCT nid FROM {node_field_data} 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 {
$query
->condition('n.status', NODE_PUBLISHED);
}
}
$nids = $query
->distinct()
->fields('n', array(
'nid',
))
->orderBy('n.changed', 'DESC')
->range(0, $number)
->addTag('node_access')
->execute()
->fetchCol();
$nodes = node_load_multiple($nids);
return $nodes ? $nodes : array();
}
function theme_node_recent_block($variables) {
$rows = array();
$output = '';
$l_options = array(
'query' => drupal_get_destination(),
);
foreach ($variables['nodes'] as $node) {
$row = array();
$node_recent_content = array(
'#theme' => 'node_recent_content',
'#node' => $node,
);
$row[] = array(
'data' => drupal_render($node_recent_content),
'class' => 'title-author',
);
if (node_access('update', $node)) {
$row[] = array(
'data' => l(t('edit'), 'node/' . $node->nid . '/edit', $l_options),
'class' => 'edit',
);
}
if (node_access('delete', $node)) {
$row[] = array(
'data' => l(t('delete'), 'node/' . $node->nid . '/delete', $l_options),
'class' => 'delete',
);
}
$rows[] = $row;
}
if ($rows) {
$table = array(
'#theme' => 'table',
'#rows' => $rows,
);
$output = drupal_render($table);
if (user_access('access content overview')) {
$more_link = array(
'#theme' => 'more_link',
'#url' => 'admin/content',
'#title' => t('Show more content'),
);
$output .= drupal_render($more_link);
}
}
return $output;
}
function theme_node_recent_content($variables) {
$node = $variables['node'];
$output = '<div class="node-title">';
$output .= l($node
->label(), 'node/' . $node->nid);
$mark = array(
'#theme' => 'mark',
'#mark_type' => node_mark($node->nid, $node->changed),
);
$output .= drupal_render($mark);
$output .= '</div><div class="node-author">';
$username = array(
'#theme' => 'username',
'#account' => user_load($node->uid),
);
$output .= drupal_render($username);
$output .= '</div>';
return $output;
}
function node_form_block_form_alter(&$form, &$form_state) {
$block = $form_state['controller']
->getEntity();
$visibility = $block
->get('visibility');
$form['visibility']['node_type'] = array(
'#type' => 'details',
'#title' => t('Content types'),
'#collapsed' => TRUE,
'#group' => 'visibility',
'#weight' => 5,
);
$form['visibility']['node_type']['types'] = array(
'#type' => 'checkboxes',
'#title' => t('Show block for specific content types'),
'#default_value' => !empty($visibility['node_type']['types']) ? $visibility['node_type']['types'] : array(),
'#options' => node_type_get_names(),
'#description' => t('Show this block only on pages that display content of the given type(s). If you select no types, there will be no type-specific limitation.'),
);
}
function node_block_access($block) {
$visibility = $block
->get('visibility');
if (!empty($visibility)) {
if (!empty($visibility['node_type']['types'])) {
$allowed_types = array_filter($visibility['node_type']['types']);
}
if (empty($allowed_types)) {
return;
}
$node = menu_get_object();
$node_types = node_type_get_types();
if (arg(0) == 'node' && arg(1) == 'add' && arg(2)) {
$node_add_arg = strtr(arg(2), '-', '_');
}
if (!empty($node)) {
return in_array($node->type, $allowed_types);
}
elseif (isset($node_add_arg) && isset($node_types[$node_add_arg])) {
return in_array($node_add_arg, $allowed_types);
}
else {
return FALSE;
}
}
}
function node_feed($nids = FALSE, $channel = array()) {
global $base_url;
$language_content = language(Language::TYPE_CONTENT);
$rss_config = config('system.rss');
if ($nids === FALSE) {
$nids = db_select('node_field_data', 'n')
->distinct()
->fields('n', array(
'nid',
))
->condition('n.promote', 1)
->condition('n.status', 1)
->orderBy('n.created', 'DESC')
->range(0, $rss_config
->get('items.limit'))
->addTag('node_access')
->execute()
->fetchCol();
}
$item_length = $rss_config
->get('items.view_mode');
$namespaces = array(
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
);
$nodes = node_load_multiple($nids);
$items = '';
foreach ($nodes as $node) {
$item_text = '';
$node->link = url("node/{$node->nid}", array(
'absolute' => TRUE,
));
$node->rss_namespaces = array();
$node->rss_elements = array(
array(
'key' => 'pubDate',
'value' => gmdate('r', $node->created),
),
array(
'key' => 'dc:creator',
'value' => $node->name,
),
array(
'key' => 'guid',
'value' => $node->nid . ' at ' . $base_url,
'attributes' => array(
'isPermaLink' => 'false',
),
),
);
$build = node_view($node, 'rss');
unset($build['#theme']);
if (!empty($node->rss_namespaces)) {
$namespaces = array_merge($namespaces, $node->rss_namespaces);
}
if ($item_length != 'title') {
$build['links']['#weight'] = 1000;
$item_text .= drupal_render($build);
}
$items .= format_rss_item($node
->label(), $node->link, $item_text, $node->rss_elements);
}
$channel_defaults = array(
'version' => '2.0',
'title' => config('system.site')
->get('name'),
'link' => $base_url,
'description' => $rss_config
->get('channel.description'),
'language' => $language_content->langcode,
);
$channel_extras = array_diff_key($channel, $channel_defaults);
$channel = array_merge($channel_defaults, $channel);
$output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$output .= "<rss version=\"" . $channel["version"] . "\" xml:base=\"" . $base_url . "\" " . new Attribute($namespaces) . ">\n";
$output .= format_rss_channel($channel['title'], $channel['link'], $channel['description'], $items, $channel['language'], $channel_extras);
$output .= "</rss>\n";
return new Response($output, 200, array(
'Content-Type' => 'application/rss+xml; charset=utf-8',
));
}
function node_view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) {
return entity_view($node, $view_mode, $langcode);
}
function node_view_multiple($nodes, $view_mode = 'teaser', $langcode = NULL) {
return entity_view_multiple($nodes, $view_mode, $langcode);
}
function node_page_view(EntityInterface $node) {
drupal_set_title($node
->label());
foreach ($node
->uriRelationships() as $rel) {
$uri = $node
->uri($rel);
drupal_add_html_head_link(array(
'rel' => $rel,
'href' => url($uri['path'], $uri['options']),
), TRUE);
if ($rel == 'canonical') {
drupal_add_html_head_link(array(
'rel' => 'shortlink',
'href' => url($uri['path'], array_merge($uri['options'], array(
'alias' => TRUE,
))),
), TRUE);
}
}
return node_show($node);
}
function node_update_index() {
$limit = (int) config('search.settings')
->get('index.cron_limit');
$result = db_query_range("SELECT n.nid 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 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit, array(), array(
'target' => 'slave',
));
$nids = $result
->fetchCol();
if (!$nids) {
return;
}
$counter = 0;
foreach (node_load_multiple($nids) as $node) {
$counter += count($node
->getTranslationLanguages());
if ($counter > $limit) {
break;
}
_node_index_node($node);
}
}
function _node_index_node(EntityInterface $node) {
Drupal::state()
->set('node.cron_last', $node->changed);
$languages = $node
->getTranslationLanguages();
foreach ($languages as $language) {
$build = node_view($node, 'search_index', $language->langcode);
unset($build['#theme']);
$node->rendered = drupal_render($build);
$text = '<h1>' . check_plain($node
->label($language->langcode)) . '</h1>' . $node->rendered;
$extra = module_invoke_all('node_update_index', $node, $language->langcode);
foreach ($extra as $t) {
$text .= $t;
}
search_index($node->nid, 'node', $text, $language->langcode);
}
}
function node_form_search_form_alter(&$form, $form_state) {
if (isset($form['module']) && $form['module']['#value'] == 'node' && user_access('use advanced search')) {
$form['advanced'] = array(
'#type' => 'details',
'#title' => t('Advanced search'),
'#collapsed' => TRUE,
'#attributes' => array(
'class' => array(
'search-advanced',
),
),
);
$form['advanced']['keywords-fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Keywords'),
'#collapsible' => FALSE,
);
$form['advanced']['keywords'] = array(
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
);
$form['advanced']['keywords-fieldset']['keywords']['or'] = array(
'#type' => 'textfield',
'#title' => t('Containing any of the words'),
'#size' => 30,
'#maxlength' => 255,
);
$form['advanced']['keywords-fieldset']['keywords']['phrase'] = array(
'#type' => 'textfield',
'#title' => t('Containing the phrase'),
'#size' => 30,
'#maxlength' => 255,
);
$form['advanced']['keywords-fieldset']['keywords']['negative'] = array(
'#type' => 'textfield',
'#title' => t('Containing none of the words'),
'#size' => 30,
'#maxlength' => 255,
);
$types = array_map('check_plain', node_type_get_names());
$form['advanced']['types-fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Types'),
'#collapsible' => FALSE,
);
$form['advanced']['types-fieldset']['type'] = array(
'#type' => 'checkboxes',
'#title' => t('Only of the type(s)'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $types,
);
$form['advanced']['submit'] = array(
'#type' => 'submit',
'#value' => t('Advanced search'),
'#prefix' => '<div class="action">',
'#suffix' => '</div>',
'#weight' => 100,
);
$language_options = array();
foreach (language_list(Language::STATE_ALL) as $langcode => $language) {
$language_options[$langcode] = $language->locked ? t('- @name -', array(
'@name' => $language->name,
)) : $language->name;
}
if (count($language_options) > 1) {
$form['advanced']['lang-fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Languages'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['advanced']['lang-fieldset']['language'] = array(
'#type' => 'checkboxes',
'#title' => t('Languages'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $language_options,
);
}
$form['#validate'][] = 'node_search_validate';
}
}
function node_search_validate($form, &$form_state) {
$keys = $form_state['values']['processed_keys'];
if (isset($form_state['values']['type']) && is_array($form_state['values']['type'])) {
$form_state['values']['type'] = array_filter($form_state['values']['type']);
if (count($form_state['values']['type'])) {
$keys = search_expression_insert($keys, 'type', implode(',', array_keys($form_state['values']['type'])));
}
}
if (isset($form_state['values']['term']) && is_array($form_state['values']['term']) && count($form_state['values']['term'])) {
$keys = search_expression_insert($keys, 'term', implode(',', $form_state['values']['term']));
}
if (isset($form_state['values']['language']) && is_array($form_state['values']['language'])) {
$languages = array_filter($form_state['values']['language']);
if (count($languages)) {
$keys = search_expression_insert($keys, 'language', implode(',', $languages));
}
}
if ($form_state['values']['or'] != '') {
if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state['values']['or'], $matches)) {
$keys .= ' ' . implode(' OR ', $matches[1]);
}
}
if ($form_state['values']['negative'] != '') {
if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state['values']['negative'], $matches)) {
$keys .= ' -' . implode(' -', $matches[1]);
}
}
if ($form_state['values']['phrase'] != '') {
$keys .= ' "' . str_replace('"', ' ', $form_state['values']['phrase']) . '"';
}
if (!empty($keys)) {
form_set_value($form['basic']['processed_keys'], trim($keys), $form_state);
}
}
function node_form_system_site_information_settings_form_alter(&$form, &$form_state, $form_id) {
$form['front_page']['default_nodes_main'] = array(
'#type' => 'select',
'#title' => t('Number of posts on front page'),
'#default_value' => config('node.settings')
->get('items_per_page'),
'#options' => drupal_map_assoc(array(
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
15,
20,
25,
30,
)),
'#access' => config('system.site')
->get('page.front') == 'node',
'#description' => t('The maximum number of posts displayed on overview pages such as the front page.'),
);
$form['#submit'][] = 'node_form_system_site_information_settings_form_submit';
}
function node_form_system_site_information_settings_form_submit($form, &$form_state) {
config('node.settings')
->set('items_per_page', $form_state['values']['default_nodes_main'])
->save();
}
function node_form_system_themes_admin_form_alter(&$form, &$form_state, $form_id) {
$form['admin_theme']['node_admin_theme'] = array(
'#type' => 'checkbox',
'#title' => t('Use the administration theme when editing or creating content'),
'#default_value' => variable_get('node_admin_theme', '0'),
);
$form['#submit'][] = 'node_form_system_themes_admin_form_submit';
}
function node_form_system_themes_admin_form_submit($form, &$form_state) {
variable_set('node_admin_theme', $form_state['values']['node_admin_theme']);
}
function node_access($op, $node, $account = NULL, $langcode = NULL) {
if (!$node instanceof EntityInterface) {
$type = is_object($node) ? $node->type : $node;
$node = entity_create('node', array(
'type' => $type,
));
}
if (empty($langcode)) {
$langcode = $node->langcode;
if (module_exists('language')) {
$node_translations = $node
->getTranslationLanguages();
$content_negotiation_langcode = language(Language::TYPE_CONTENT)->langcode;
if (isset($node_translations[$content_negotiation_langcode])) {
$langcode = $content_negotiation_langcode;
}
}
}
if ($account && !$account instanceof UserInterface) {
$account = user_load($account->uid);
}
return entity_access_controller('node')
->access($node, $op, $langcode, $account);
}
function node_node_access($node, $op, $account) {
$type = $node
->bundle();
$configured_types = node_permissions_get_configured_types();
if (isset($configured_types[$type])) {
if ($op == 'create' && user_access('create ' . $type . ' content', $account)) {
return NODE_ACCESS_ALLOW;
}
if ($op == 'update') {
if (user_access('edit any ' . $type . ' content', $account) || user_access('edit own ' . $type . ' content', $account) && $account->uid == $node->uid) {
return NODE_ACCESS_ALLOW;
}
}
if ($op == 'delete') {
if (user_access('delete any ' . $type . ' content', $account) || user_access('delete own ' . $type . ' content', $account) && $account->uid == $node->uid) {
return NODE_ACCESS_ALLOW;
}
}
}
return NODE_ACCESS_IGNORE;
}
function node_list_permissions($type) {
$perms = array(
"create {$type->type} content" => array(
'title' => t('%type_name: Create new content', array(
'%type_name' => $type->name,
)),
),
"edit own {$type->type} content" => array(
'title' => t('%type_name: Edit own content', array(
'%type_name' => $type->name,
)),
),
"edit any {$type->type} content" => array(
'title' => t('%type_name: Edit any content', array(
'%type_name' => $type->name,
)),
),
"delete own {$type->type} content" => array(
'title' => t('%type_name: Delete own content', array(
'%type_name' => $type->name,
)),
),
"delete any {$type->type} content" => array(
'title' => t('%type_name: Delete any content', array(
'%type_name' => $type->name,
)),
),
"view {$type->type} revisions" => array(
'title' => t('%type_name: View revisions', array(
'%type_name' => $type->name,
)),
),
"revert {$type->type} revisions" => array(
'title' => t('%type_name: Revert revisions', array(
'%type_name' => $type->name,
)),
'description' => t('Role requires permission <em>view revisions</em> and <em>edit rights</em> for nodes in question, or <em>administer nodes</em>.'),
),
"delete {$type->type} revisions" => array(
'title' => t('%type_name: Delete revisions', array(
'%type_name' => $type->name,
)),
'description' => t('Role requires permission to <em>view revisions</em> and <em>delete rights</em> for nodes in question, or <em>administer nodes</em>.'),
),
);
return $perms;
}
function node_permissions_get_configured_types() {
$configured_types = array();
foreach (node_type_get_types() as $name => $type) {
if (variable_get('node_permissions_' . $name, 1)) {
$configured_types[$name] = $type;
}
}
return $configured_types;
}
function node_access_grants($op, $account = NULL) {
if (!isset($account)) {
$account = $GLOBALS['user'];
}
$grants = module_invoke_all('node_grants', $account, $op);
drupal_alter('node_grants', $grants, $account, $op);
return array_merge(array(
'all' => array(
0,
),
), $grants);
}
function node_access_view_all_nodes($account = NULL) {
global $user;
if (!$account) {
$account = $user;
}
$access =& drupal_static(__FUNCTION__);
if (isset($access[$account->uid])) {
return $access[$account->uid];
}
if (!module_implements('node_grants')) {
$access[$account->uid] = TRUE;
}
else {
$query = db_select('node_access');
$query
->addExpression('COUNT(*)');
$query
->condition('nid', 0)
->condition('grant_view', 1, '>=');
$grants = db_or();
foreach (node_access_grants('view', $account) as $realm => $gids) {
foreach ($gids as $gid) {
$grants
->condition(db_and()
->condition('gid', $gid)
->condition('realm', $realm));
}
}
if (count($grants) > 0) {
$query
->condition($grants);
}
$access[$account->uid] = $query
->execute()
->fetchField();
}
return $access[$account->uid];
}
function node_query_node_access_alter(AlterableInterface $query) {
global $user;
if (!($account = $query
->getMetaData('account'))) {
$account = $user;
}
if (!($op = $query
->getMetaData('op'))) {
$op = 'view';
}
if (!($langcode = $query
->getMetaData('langcode'))) {
$langcode = FALSE;
}
if (user_access('bypass node access', $account)) {
return;
}
if (!count(module_implements('node_grants'))) {
return;
}
if ($op == 'view' && node_access_view_all_nodes($account)) {
return;
}
$tables = $query
->getTables();
$base_table = $query
->getMetaData('base_table');
if (!$base_table) {
foreach ($tables as $table_info) {
if (!$table_info instanceof SelectInterface) {
$table = $table_info['table'];
if ($table == 'node' || $table == 'node_field_data') {
$base_table = $table;
break;
}
}
}
if (!$base_table) {
throw new Exception(t('Query tagged for node access but there is no node table, specify the base_table using meta data.'));
}
}
$grants = node_access_grants($op, $account);
$base_table_found = FALSE;
foreach ($tables as $nalias => $tableinfo) {
$table = $tableinfo['table'];
if (!$table instanceof SelectInterface && $table == $base_table) {
$base_table_found = TRUE;
$subquery = db_select('node_access', 'na')
->fields('na', array(
'nid',
));
$grant_conditions = db_or();
foreach ($grants as $realm => $gids) {
foreach ($gids as $gid) {
$grant_conditions
->condition(db_and()
->condition('na.gid', $gid)
->condition('na.realm', $realm));
}
}
if (count($grant_conditions
->conditions())) {
$subquery
->condition($grant_conditions);
}
$subquery
->condition('na.grant_' . $op, 1, '>=');
if (language_multilingual()) {
if ($langcode === FALSE) {
$subquery
->condition('na.fallback', 1, '=');
}
else {
$subquery
->condition('na.langcode', $langcode, '=');
}
}
$field = 'nid';
$subquery
->where("{$nalias}.{$field} = na.nid");
$query
->exists($subquery);
}
}
if (!$base_table_found) {
throw new Exception(t('Query tagged for node access but the defined base_table @base_table was not found', array(
'@base_table' => $base_table,
)));
}
}
function node_access_acquire_grants(EntityInterface $node, $delete = TRUE) {
$grants = module_invoke_all('node_access_records', $node);
drupal_alter('node_access_records', $grants, $node);
if (empty($grants) && !empty($node->status)) {
$grants[] = array(
'realm' => 'all',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
);
}
_node_access_write_grants($node, $grants, NULL, $delete);
}
function _node_access_write_grants(EntityInterface $node, $grants, $realm = NULL, $delete = TRUE) {
if ($delete) {
$query = db_delete('node_access')
->condition('nid', $node->nid);
if ($realm) {
$query
->condition('realm', array(
$realm,
'all',
), 'IN');
}
$query
->execute();
}
if (!empty($grants) && count(module_implements('node_grants'))) {
$query = db_insert('node_access')
->fields(array(
'nid',
'langcode',
'fallback',
'realm',
'gid',
'grant_view',
'grant_update',
'grant_delete',
));
foreach ($grants as $grant) {
if ($realm && $realm != $grant['realm']) {
continue;
}
if (isset($grant['langcode'])) {
$grant_languages = array(
$grant['langcode'] => language_load($grant['langcode']),
);
}
else {
$grant_languages = $node
->getTranslationLanguages(TRUE);
}
foreach ($grant_languages as $grant_langcode => $grant_language) {
if ($grant['grant_view'] || $grant['grant_update'] || $grant['grant_delete']) {
$grant['nid'] = $node->nid;
$grant['langcode'] = $grant_langcode;
if ($grant['langcode'] == $node->langcode) {
$grant['fallback'] = 1;
}
else {
$grant['fallback'] = 0;
}
$query
->values($grant);
}
}
}
$query
->execute();
}
}
function node_access_needs_rebuild($rebuild = NULL) {
if (!isset($rebuild)) {
return Drupal::state()
->get('node.node_access_needs_rebuild') ?: FALSE;
}
elseif ($rebuild) {
Drupal::state()
->set('node.node_access_needs_rebuild', TRUE);
}
else {
Drupal::state()
->delete('node.node_access_needs_rebuild');
}
}
function node_access_rebuild($batch_mode = FALSE) {
db_delete('node_access')
->execute();
if (count(module_implements('node_grants'))) {
if ($batch_mode) {
$batch = array(
'title' => t('Rebuilding content access permissions'),
'operations' => array(
array(
'_node_access_rebuild_batch_operation',
array(),
),
),
'finished' => '_node_access_rebuild_batch_finished',
);
batch_set($batch);
}
else {
drupal_set_time_limit(240);
$nids = db_query("SELECT nid FROM {node} ORDER BY nid DESC")
->fetchCol();
foreach ($nids as $nid) {
$node = node_load($nid, TRUE);
if (!empty($node)) {
node_access_acquire_grants($node);
}
}
}
}
else {
db_insert('node_access')
->fields(array(
'nid' => 0,
'realm' => 'all',
'gid' => 0,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
))
->execute();
}
if (!isset($batch)) {
drupal_set_message(t('Content permissions have been rebuilt.'));
node_access_needs_rebuild(FALSE);
cache_invalidate_tags(array(
'content' => TRUE,
));
}
}
function _node_access_rebuild_batch_operation(&$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
$context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')
->fetchField();
}
$limit = 20;
$nids = db_query_range("SELECT nid FROM {node} WHERE nid > :nid ORDER BY nid ASC", 0, $limit, array(
':nid' => $context['sandbox']['current_node'],
))
->fetchCol();
$nodes = node_load_multiple($nids, TRUE);
foreach ($nodes as $nid => $node) {
if (!empty($node)) {
node_access_acquire_grants($node);
}
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $nid;
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
function _node_access_rebuild_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('The content access permissions have been rebuilt.'));
node_access_needs_rebuild(FALSE);
}
else {
drupal_set_message(t('The content access permissions have not been properly rebuilt.'), 'error');
}
cache_invalidate_tags(array(
'content' => TRUE,
));
}
function node_content_form(EntityInterface $node, $form_state) {
$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;
}
function node_requirements($phase) {
$requirements = array();
if ($phase === 'runtime') {
$grant_count = db_query('SELECT COUNT(*) FROM {node_access}')
->fetchField();
if ($grant_count != 1 || count(module_implements('node_grants')) > 0) {
$value = format_plural($grant_count, 'One permission in use', '@count permissions in use', array(
'@count' => $grant_count,
));
}
else {
$value = t('Disabled');
}
$description = t('If the site is experiencing problems with permissions to content, you may have to rebuild the permissions cache. Rebuilding will remove all privileges to content and replace them with permissions based on the current modules and settings. Rebuilding may take some time if there is a lot of content or complex permission settings. After rebuilding has completed, content will automatically use the new permissions.');
$requirements['node_access'] = array(
'title' => t('Node Access Permissions'),
'value' => $value,
'description' => $description . ' ' . l(t('Rebuild permissions'), 'admin/reports/status/rebuild'),
);
}
return $requirements;
}
function node_modules_enabled($modules) {
if (!node_access_needs_rebuild() && array_intersect($modules, module_implements('node_grants'))) {
node_access_needs_rebuild(TRUE);
}
}
function node_modules_disabled($modules) {
foreach ($modules as $module) {
if (!node_access_needs_rebuild() && (module_hook($module, 'node_grants') || $module == 'language')) {
node_access_needs_rebuild(TRUE);
}
}
if (node_access_needs_rebuild() && count(module_implements('node_grants')) == 0) {
node_access_rebuild();
}
}
function node_file_download_access($field, EntityInterface $entity, File $file) {
if ($entity
->entityType() == 'node') {
return node_access('view', $entity);
}
}
function node_language_delete($language) {
db_update('node')
->fields(array(
'langcode' => '',
))
->condition('langcode', $language->langcode)
->execute();
}
function node_library_info() {
$libraries['drupal.node'] = array(
'title' => 'Node',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'node') . '/node.js' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
array(
'system',
'drupalSettings',
),
array(
'system',
'drupal.form',
),
),
);
$libraries['drupal.node.preview'] = array(
'title' => 'Node preview',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'node') . '/node.preview.js' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
),
);
$libraries['drupal.content_types'] = array(
'title' => 'Content types',
'version' => VERSION,
'js' => array(
drupal_get_path('module', 'node') . '/content_types.js' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
array(
'system',
'drupal.form',
),
),
);
return $libraries;
}
function node_system_info_alter(&$info, $file, $type) {
if ($type == 'module' && $file->name == 'translation') {
$info['hidden'] = !module_exists('translation') && config('system.module.disabled')
->get('translation') === NULL;
}
}