1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468 |
- <?php
- use Drupal\Component\Utility\Environment;
- use Drupal\Component\Utility\Xss;
- use Drupal\Core\Access\AccessResult;
- use Drupal\Core\Cache\Cache;
- use Drupal\Core\Database\Query\AlterableInterface;
- use Drupal\Core\Database\Query\SelectInterface;
- use Drupal\Core\Database\StatementInterface;
- use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Link;
- use Drupal\Core\Render\Element;
- use Drupal\Core\Routing\RouteMatchInterface;
- use Drupal\Core\Session\AccountInterface;
- use Drupal\Core\Template\Attribute;
- use Drupal\Core\Url;
- use Drupal\field\Entity\FieldConfig;
- use Drupal\field\Entity\FieldStorageConfig;
- use Drupal\language\ConfigurableLanguageInterface;
- use Drupal\node\Entity\Node;
- use Drupal\node\Entity\NodeType;
- use Drupal\node\NodeInterface;
- use Drupal\node\NodeTypeInterface;
- 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;
- function node_help($route_name, RouteMatchInterface $route_match) {
-
-
-
- if ($route_name != 'node.configure_rebuild_confirm' && $route_name != 'system.batch_page.normal' && $route_name != 'help.page.node' && $route_name != 'help.main'
- && \Drupal::currentUser()->hasPermission('access administration pages') && node_access_needs_rebuild()) {
- if ($route_name == 'system.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>.', [':node_access_rebuild' => Url::fromRoute('node.configure_rebuild_confirm')->toString()]);
- }
- \Drupal::messenger()->addError($message);
- }
- switch ($route_name) {
- case 'help.page.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 <a href=":node">online documentation for the Node module</a>.', [':node' => 'https://www.drupal.org/documentation/modules/node', ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '</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.', [':content-type' => Url::fromRoute('entity.node_type.collection')->toString()]) . '</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 gives you the flexibility to add <a href=":field">fields</a> and configure default settings that suit the differing needs of various site content.', [':content-new' => Url::fromRoute('node.type_add')->toString(), ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '</dd>';
- $output .= '<dt>' . t('Administering content') . '</dt>';
- $output .= '<dd>' . t('The <a href=":content">Content</a> page lists your content, allowing you add new content, filter, edit or delete existing content, or perform bulk operations on existing content.', [':content' => Url::fromRoute('system.admin_content')->toString()]) . '</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>.', [':permissions' => Url::fromRoute('user.admin_permissions', [], ['fragment' => 'module-node'])->toString()]) . '</dd>';
- $output .= '</dl>';
- return $output;
- case 'node.type_add':
- return '<p>' . t('Individual content types can have different fields, behaviors, and permissions assigned to them.') . '</p>';
- case 'entity.entity_form_display.node.default':
- case 'entity.entity_form_display.node.form_mode':
- $type = $route_match->getParameter('node_type');
- return '<p>' . t('Content items can be edited using different form modes. Here, you can define which fields are shown and hidden when %type content is edited in each form mode, and define how the field form widgets are displayed in each form mode.', ['%type' => $type->label()]) . '</p>';
- case 'entity.entity_view_display.node.default':
- case 'entity.entity_view_display.node.view_mode':
- $type = $route_match->getParameter('node_type');
- 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.', ['%type' => $type->label()]) . '</p>';
- case 'entity.node.version_history':
- return '<p>' . t('Revisions allow you to track differences between multiple versions of your content, and revert to older versions.') . '</p>';
- case 'entity.node.edit_form':
- $node = $route_match->getParameter('node');
- $type = NodeType::load($node->getType());
- $help = $type->getHelp();
- return (!empty($help) ? Xss::filterAdmin($help) : '');
- case 'node.add':
- $type = $route_match->getParameter('node_type');
- $help = $type->getHelp();
- return (!empty($help) ? Xss::filterAdmin($help) : '');
- }
- }
- function node_theme() {
- return [
- 'node' => [
- 'render element' => 'elements',
- ],
- 'node_add_list' => [
- 'variables' => ['content' => NULL],
- ],
- 'node_edit_form' => [
- 'render element' => 'form',
- ],
- 'field__node__title' => [
- 'base hook' => 'field',
- ],
- 'field__node__uid' => [
- 'base hook' => 'field',
- ],
- 'field__node__created' => [
- 'base hook' => 'field',
- ],
- ];
- }
- function node_entity_view_display_alter(EntityViewDisplayInterface $display, $context) {
- if ($context['entity_type'] == 'node') {
-
- if ($context['view_mode'] == 'search_index') {
- foreach ($display->getComponents() as $name => $options) {
- if (isset($options['label'])) {
- $options['label'] = 'hidden';
- $display->setComponent($name, $options);
- }
- }
- }
- }
- }
- function node_title_list(StatementInterface $result, $title = NULL) {
- $items = [];
- $num_rows = FALSE;
- $nids = [];
- foreach ($result as $row) {
-
-
- $nids[] = $row->nid;
- $options = !empty($row->comment_count) ? ['attributes' => ['title' => \Drupal::translation()->formatPlural($row->comment_count, '1 comment', '@count comments')]] : [];
- $items[] = Link::fromTextAndUrl($row->title, Url::fromRoute('entity.node.canonical', ['node' => $row->nid], $options))->toString();
- $num_rows = TRUE;
- }
- return $num_rows ? ['#theme' => 'item_list__node', '#items' => $items, '#title' => $title, '#cache' => ['tags' => Cache::mergeTags(['node_list'], Cache::buildTags('node', $nids))]] : FALSE;
- }
- function node_mark($nid, $timestamp) {
- $cache = &drupal_static(__FUNCTION__, []);
- if (\Drupal::currentUser()->isAnonymous() || !\Drupal::moduleHandler()->moduleExists('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 NodeType::loadMultiple();
- }
- function node_type_get_names() {
- return array_map(function ($bundle_info) {
- return $bundle_info['label'];
- }, \Drupal::service('entity_type.bundle.info')->getBundleInfo('node'));
- }
- function node_get_type_label(NodeInterface $node) {
- $type = NodeType::load($node->bundle());
- return $type ? $type->label() : FALSE;
- }
- function node_type_get_description(NodeTypeInterface $node_type) {
- return $node_type->getDescription();
- }
- function node_type_load($name) {
- @trigger_error('node_type_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\node\Entity\NodeType::load(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
- return NodeType::load($name);
- }
- function node_add_body_field(NodeTypeInterface $type, $label = 'Body') {
-
- $field_storage = FieldStorageConfig::loadByName('node', 'body');
- $field = FieldConfig::loadByName('node', $type->id(), 'body');
- if (empty($field)) {
- $field = FieldConfig::create([
- 'field_storage' => $field_storage,
- 'bundle' => $type->id(),
- 'label' => $label,
- 'settings' => ['display_summary' => TRUE],
- ]);
- $field->save();
-
- $display_repository = \Drupal::service('entity_display.repository');
-
- $display_repository->getFormDisplay('node', $type->id())
- ->setComponent('body', [
- 'type' => 'text_textarea_with_summary',
- ])
- ->save();
-
- $display_repository->getViewDisplay('node', $type->id())
- ->setComponent('body', [
- 'label' => 'hidden',
- 'type' => 'text_default',
- ])
- ->save();
-
-
- $view_modes = \Drupal::service('entity_display.repository')->getViewModes('node');
- if (isset($view_modes['teaser'])) {
- $display_repository->getViewDisplay('node', $type->id(), 'teaser')
- ->setComponent('body', [
- 'label' => 'hidden',
- 'type' => 'text_summary_or_trimmed',
- ])
- ->save();
- }
- }
- return $field;
- }
- function node_entity_extra_field_info() {
- $extra = [];
- $description = t('Node module element');
- foreach (NodeType::loadMultiple() as $bundle) {
- $extra['node'][$bundle->id()]['display']['links'] = [
- 'label' => t('Links'),
- 'description' => $description,
- 'weight' => 100,
- 'visible' => TRUE,
- ];
- }
- return $extra;
- }
- function node_type_update_nodes($old_id, $new_id) {
- return \Drupal::entityTypeManager()->getStorage('node')->updateType($old_id, $new_id);
- }
- function node_load_multiple(array $nids = NULL, $reset = FALSE) {
- @trigger_error('node_load_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\node\Entity\Node::loadMultiple(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
- if ($reset) {
- \Drupal::entityTypeManager()->getStorage('node')->resetCache($nids);
- }
- return Node::loadMultiple($nids);
- }
- function node_load($nid = NULL, $reset = FALSE) {
- @trigger_error('node_load() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal\node\Entity\Node::load(). See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
- if ($reset) {
- \Drupal::entityTypeManager()->getStorage('node')->resetCache([$nid]);
- }
- return Node::load($nid);
- }
- function node_revision_load($vid = NULL) {
- return \Drupal::entityTypeManager()->getStorage('node')->loadRevision($vid);
- }
- function node_revision_delete($revision_id) {
- \Drupal::entityTypeManager()->getStorage('node')->deleteRevision($revision_id);
- }
- function node_is_page(NodeInterface $node) {
- $route_match = \Drupal::routeMatch();
- if ($route_match->getRouteName() == 'entity.node.canonical') {
- $page_node = $route_match->getParameter('node');
- }
- return (!empty($page_node) ? $page_node->id() == $node->id() : FALSE);
- }
- function template_preprocess_node_add_list(&$variables) {
- $variables['types'] = [];
- if (!empty($variables['content'])) {
- foreach ($variables['content'] as $type) {
- $variables['types'][$type->id()] = [
- 'type' => $type->id(),
- 'add_link' => Link::fromTextAndUrl($type->label(), Url::fromRoute('node.add', ['node_type' => $type->id()]))->toString(),
- 'description' => [
- '#markup' => $type->getDescription(),
- ],
- ];
- }
- }
- }
- function node_preprocess_html(&$variables) {
-
-
- if (($node = \Drupal::routeMatch()->getParameter('node')) || ($node = \Drupal::routeMatch()->getParameter('node_preview'))) {
- if ($node instanceof NodeInterface) {
- $variables['node_type'] = $node->getType();
- }
- }
- }
- function node_preprocess_block(&$variables) {
- if ($variables['configuration']['provider'] == 'node') {
- switch ($variables['elements']['#plugin_id']) {
- case 'node_syndicate_block':
- $variables['attributes']['role'] = 'complementary';
- break;
- }
- }
- }
- function node_theme_suggestions_node(array $variables) {
- $suggestions = [];
- $node = $variables['elements']['#node'];
- $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
- $suggestions[] = 'node__' . $sanitized_view_mode;
- $suggestions[] = 'node__' . $node->bundle();
- $suggestions[] = 'node__' . $node->bundle() . '__' . $sanitized_view_mode;
- $suggestions[] = 'node__' . $node->id();
- $suggestions[] = 'node__' . $node->id() . '__' . $sanitized_view_mode;
- return $suggestions;
- }
- 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'];
- $skip_custom_preprocessing = $node->getEntityType()->get('enable_base_field_custom_preprocess_skipping');
-
-
-
-
-
-
- $submitted_configurable = $node->getFieldDefinition('created')->isDisplayConfigurable('view') || $node->getFieldDefinition('uid')->isDisplayConfigurable('view');
- if (!$skip_custom_preprocessing || !$submitted_configurable) {
- $variables['date'] = \Drupal::service('renderer')->render($variables['elements']['created']);
- unset($variables['elements']['created']);
- $variables['author_name'] = \Drupal::service('renderer')->render($variables['elements']['uid']);
- unset($variables['elements']['uid']);
- }
- if (isset($variables['elements']['title']) && (!$skip_custom_preprocessing || !$node->getFieldDefinition('title')->isDisplayConfigurable('view'))) {
- $variables['label'] = $variables['elements']['title'];
- unset($variables['elements']['title']);
- }
- $variables['url'] = !$node->isNew() ? $node->toUrl('canonical')->toString() : NULL;
-
-
-
- $variables['page'] = ($variables['view_mode'] == 'full' && (node_is_page($node)) || (isset($node->in_preview) && in_array($node->preview_view_mode, ['full', 'default'])));
-
- $variables += ['content' => []];
- foreach (Element::children($variables['elements']) as $key) {
- $variables['content'][$key] = $variables['elements'][$key];
- }
- if (isset($variables['date'])) {
-
-
-
-
-
- $node_type = $node->type->entity;
-
- $variables['author_attributes'] = new Attribute();
- $variables['display_submitted'] = $node_type->displaySubmitted();
- if ($variables['display_submitted']) {
- if (theme_get_setting('features.node_user_picture')) {
-
-
-
-
- $variables['author_picture'] = \Drupal::entityTypeManager()
- ->getViewBuilder('user')
- ->view($node->getOwner(), 'compact');
- }
- }
- }
-
- $variables['attributes']['role'] = 'article';
- }
- function node_cron() {
-
-
-
- if (\Drupal::moduleHandler()->moduleExists('search')) {
- $min_alias = 'min_created';
- $max_alias = 'max_created';
- $result = \Drupal::entityQueryAggregate('node')
- ->aggregate('created', 'MIN', NULL, $min_alias)
- ->aggregate('created', 'MAX', NULL, $max_alias)
- ->execute();
- if (isset($result[0])) {
-
- $array = [
- 'min_created' => $result[0][$min_alias],
- 'max_created' => $result[0][$max_alias],
- ];
- \Drupal::state()->set('node.min_max_update_time', $array);
- }
- }
- }
- function node_ranking() {
-
- $ranking = [
- 'relevance' => [
- 'title' => t('Keyword relevance'),
-
- 'score' => 'i.relevance',
- ],
- 'sticky' => [
- 'title' => t('Content is sticky at top of lists'),
-
- 'score' => 'n.sticky',
- ],
- 'promote' => [
- 'title' => t('Content is promoted to the front page'),
-
- 'score' => 'n.promote',
- ],
- ];
-
-
- if ($node_min_max = \Drupal::state()->get('node.min_max_update_time')) {
- $ranking['recent'] = [
- 'title' => t('Recently created'),
-
- 'score' => 'EXP(-5 * (1 - (n.created - :node_oldest) / :node_range))',
- 'arguments' => [
- ':node_oldest' => $node_min_max['min_created'],
- ':node_range' => max($node_min_max['max_created'] - $node_min_max['min_created'], 1),
- ],
- ];
- }
- return $ranking;
- }
- function node_user_cancel($edit, UserInterface $account, $method) {
- switch ($method) {
- case 'user_cancel_block_unpublish':
-
- $nids = \Drupal::entityQuery('node')
- ->condition('uid', $account->id())
- ->execute();
- module_load_include('inc', 'node', 'node.admin');
- node_mass_update($nids, ['status' => 0], NULL, TRUE);
- break;
- case 'user_cancel_reassign':
-
- module_load_include('inc', 'node', 'node.admin');
- $vids = \Drupal::entityTypeManager()->getStorage('node')->userRevisionIds($account);
- node_mass_update($vids, [
- 'uid' => 0,
- 'revision_uid' => 0,
- ], NULL, TRUE, TRUE);
- break;
- }
- }
- function node_user_predelete($account) {
-
-
- $nids = \Drupal::entityQuery('node')
- ->condition('uid', $account->id())
- ->accessCheck(FALSE)
- ->execute();
-
- $storage_controller = \Drupal::entityTypeManager()->getStorage('node');
- $nodes = $storage_controller->loadMultiple($nids);
- $storage_controller->delete($nodes);
- $revisions = $storage_controller->userRevisionIds($account);
- foreach ($revisions as $revision) {
- node_revision_delete($revision);
- }
- }
- function node_get_recent($number = 10) {
- $account = \Drupal::currentUser();
- $query = \Drupal::entityQuery('node');
- if (!$account->hasPermission('bypass node access')) {
-
-
-
- $access_query = \Drupal::entityQuery('node')
- ->condition('uid', $account->id())
- ->condition('status', NodeInterface::NOT_PUBLISHED);
- if ($account->hasPermission('view own unpublished content') && ($own_unpublished = $access_query->execute())) {
- $query->orConditionGroup()
- ->condition('status', NodeInterface::PUBLISHED)
- ->condition('nid', $own_unpublished, 'IN');
- }
- else {
-
- $query->condition('status', NodeInterface::PUBLISHED);
- }
- }
- $nids = $query
- ->sort('changed', 'DESC')
- ->range(0, $number)
- ->addTag('node_access')
- ->execute();
- $nodes = Node::loadMultiple($nids);
- return $nodes ? $nodes : [];
- }
- function node_view(NodeInterface $node, $view_mode = 'full', $langcode = NULL) {
- @trigger_error("node_view() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('node')->view() instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED);
- return \Drupal::entityTypeManager()
- ->getViewBuilder('node')
- ->view($node, $view_mode, $langcode);
- }
- function node_view_multiple($nodes, $view_mode = 'teaser', $langcode = NULL) {
- @trigger_error("node_view_multiple() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder('node')->viewMultiple() instead. See https://www.drupal.org/node/3033656", E_USER_DEPRECATED);
- return \Drupal::entityTypeManager()
- ->getViewBuilder('node')
- ->viewMultiple($nodes, $view_mode, $langcode);
- }
- function node_page_top(array &$page) {
-
- $route_match = \Drupal::routeMatch();
- if ($route_match->getRouteName() == 'entity.node.preview') {
- $page['page_top']['node_preview'] = [
- '#type' => 'container',
- '#attributes' => [
- 'class' => ['node-preview-container', 'container-inline'],
- ],
- ];
- $form = \Drupal::formBuilder()->getForm('\Drupal\node\Form\NodePreviewForm', $route_match->getParameter('node_preview'));
- $page['page_top']['node_preview']['view_mode'] = $form;
- }
- }
- function node_form_system_themes_admin_form_alter(&$form, FormStateInterface $form_state, $form_id) {
- $form['admin_theme']['use_admin_theme'] = [
- '#type' => 'checkbox',
- '#title' => t('Use the administration theme when editing or creating content'),
- '#description' => t('Control which roles can "View the administration theme" on the <a href=":permissions">Permissions page</a>.', [':permissions' => Url::fromRoute('user.admin_permissions')->toString()]),
- '#default_value' => \Drupal::configFactory()->getEditable('node.settings')->get('use_admin_theme'),
- ];
- $form['#submit'][] = 'node_form_system_themes_admin_form_submit';
- }
- function node_form_system_themes_admin_form_submit($form, FormStateInterface $form_state) {
- \Drupal::configFactory()->getEditable('node.settings')
- ->set('use_admin_theme', $form_state->getValue('use_admin_theme'))
- ->save();
- }
- function node_node_access(NodeInterface $node, $op, AccountInterface $account) {
- $type = $node->bundle();
-
- switch ($op) {
- case 'update':
- $access = AccessResult::allowedIfHasPermission($account, 'edit any ' . $type . ' content');
- if (!$access->isAllowed() && $account->hasPermission('edit own ' . $type . ' content')) {
- $access = $access->orIf(AccessResult::allowedIf($account->id() == $node->getOwnerId())->cachePerUser()->addCacheableDependency($node));
- }
- break;
- case 'delete':
- $access = AccessResult::allowedIfHasPermission($account, 'delete any ' . $type . ' content');
- if (!$access->isAllowed() && $account->hasPermission('delete own ' . $type . ' content')) {
- $access = $access->orIf(AccessResult::allowedIf($account->id() == $node->getOwnerId()))->cachePerUser()->addCacheableDependency($node);
- }
- break;
- default:
- $access = AccessResult::neutral();
- }
- return $access;
- }
- function node_access_grants($op, AccountInterface $account) {
-
- $grants = \Drupal::moduleHandler()->invokeAll('node_grants', [$account, $op]);
-
- \Drupal::moduleHandler()->alter('node_grants', $grants, $account, $op);
- return array_merge(['all' => [0]], $grants);
- }
- function node_access_view_all_nodes($account = NULL) {
- if (!$account) {
- $account = \Drupal::currentUser();
- }
-
- $access = &drupal_static(__FUNCTION__);
- if (isset($access[$account->id()])) {
- return $access[$account->id()];
- }
-
- if (!\Drupal::moduleHandler()->getImplementations('node_grants')) {
- $access[$account->id()] = TRUE;
- }
- else {
- $access[$account->id()] = \Drupal::entityTypeManager()->getAccessControlHandler('node')->checkAllGrants($account);
- }
- return $access[$account->id()];
- }
- function node_query_node_access_alter(AlterableInterface $query) {
-
- if (!$account = $query->getMetaData('account')) {
- $account = \Drupal::currentUser();
- }
- if (!$op = $query->getMetaData('op')) {
- $op = 'view';
- }
-
-
-
- if ($account->hasPermission('bypass node access')) {
- return;
- }
- if (!count(\Drupal::moduleHandler()->getImplementations('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) {
-
- $table_mapping = \Drupal::entityTypeManager()->getStorage('node')->getTableMapping();
- $node_base_tables = $table_mapping->getTableNames();
- 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 (in_array($table, $node_base_tables)) {
- $base_table = $table;
- }
- }
- }
-
- if (!$base_table) {
- throw new Exception('Query tagged for node access but there is no node table, specify the base_table using meta data.');
- }
- }
-
- \Drupal::service('node.grant_storage')->alterQuery($query, $tables, $op, $account, $base_table);
-
-
- $request = \Drupal::requestStack()->getCurrentRequest();
- $renderer = \Drupal::service('renderer');
- if ($request->isMethodCacheable() && $renderer->hasRenderContext()) {
- $build = ['#cache' => ['contexts' => ['user.node_grants:' . $op]]];
- $renderer->render($build);
- }
- }
- 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) {
- $node_storage = \Drupal::entityTypeManager()->getStorage('node');
-
- $access_control_handler = \Drupal::entityTypeManager()->getAccessControlHandler('node');
- $access_control_handler->deleteGrants();
-
- if (count(\Drupal::moduleHandler()->getImplementations('node_grants'))) {
- if ($batch_mode) {
- $batch = [
- 'title' => t('Rebuilding content access permissions'),
- 'operations' => [
- ['_node_access_rebuild_batch_operation', []],
- ],
- 'finished' => '_node_access_rebuild_batch_finished',
- ];
- batch_set($batch);
- }
- else {
-
- Environment::setTimeLimit(240);
-
-
- $entity_query = \Drupal::entityQuery('node');
- $entity_query->sort('nid', 'DESC');
-
-
-
-
- $entity_query->accessCheck(FALSE);
- $nids = $entity_query->execute();
- foreach ($nids as $nid) {
- $node_storage->resetCache([$nid]);
- $node = Node::load($nid);
-
-
- if (!empty($node)) {
- $grants = $access_control_handler->acquireGrants($node);
- \Drupal::service('node.grant_storage')->write($node, $grants);
- }
- }
- }
- }
- else {
-
- $access_control_handler->writeDefaultGrant();
- }
- if (!isset($batch)) {
- \Drupal::messenger()->addStatus(t('Content permissions have been rebuilt.'));
- node_access_needs_rebuild(FALSE);
- }
- }
- function _node_access_rebuild_batch_operation(&$context) {
- $node_storage = \Drupal::entityTypeManager()->getStorage('node');
- if (empty($context['sandbox'])) {
-
- $context['sandbox']['progress'] = 0;
- $context['sandbox']['current_node'] = 0;
- $context['sandbox']['max'] = \Drupal::entityQuery('node')->accessCheck(FALSE)->count()->execute();
- }
-
- $limit = 20;
- $nids = \Drupal::entityQuery('node')
- ->condition('nid', $context['sandbox']['current_node'], '>')
- ->sort('nid', 'ASC')
-
-
-
-
- ->accessCheck(FALSE)
- ->range(0, $limit)
- ->execute();
- $node_storage->resetCache($nids);
- $nodes = Node::loadMultiple($nids);
- foreach ($nodes as $nid => $node) {
-
-
- if (!empty($node)) {
-
- $access_control_handler = \Drupal::entityTypeManager()->getAccessControlHandler('node');
- $grants = $access_control_handler->acquireGrants($node);
- \Drupal::service('node.grant_storage')->write($node, $grants);
- }
- $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::messenger()->addStatus(t('The content access permissions have been rebuilt.'));
- node_access_needs_rebuild(FALSE);
- }
- else {
- \Drupal::messenger()->addError(t('The content access permissions have not been properly rebuilt.'));
- }
- }
- function node_modules_installed($modules) {
-
-
- if (!node_access_needs_rebuild() && array_intersect($modules, \Drupal::moduleHandler()->getImplementations('node_grants'))) {
- node_access_needs_rebuild(TRUE);
- }
- }
- function node_modules_uninstalled($modules) {
-
-
- foreach ($modules as $module) {
-
-
-
-
-
- if (!node_access_needs_rebuild() && (\Drupal::moduleHandler()->implementsHook($module, 'node_grants') || $module == 'language')) {
- node_access_needs_rebuild(TRUE);
- }
- }
-
-
- if (node_access_needs_rebuild() && count(\Drupal::moduleHandler()->getImplementations('node_grants')) == 0) {
- node_access_rebuild();
- }
- }
- function node_configurable_language_delete(ConfigurableLanguageInterface $language) {
-
- \Drupal::entityTypeManager()->getStorage('node')->clearRevisionsLanguage($language);
- }
- function node_reindex_node_search($nid) {
- if (\Drupal::moduleHandler()->moduleExists('search')) {
-
- \Drupal::service('search.index')->markForReindex('node_search', $nid);
- }
- }
- function node_comment_insert($comment) {
-
- if ($comment->getCommentedEntityTypeId() == 'node') {
- node_reindex_node_search($comment->getCommentedEntityId());
- }
- }
- function node_comment_update($comment) {
-
- if ($comment->getCommentedEntityTypeId() == 'node') {
- node_reindex_node_search($comment->getCommentedEntityId());
- }
- }
- function node_comment_delete($comment) {
-
- if ($comment->getCommentedEntityTypeId() == 'node') {
- node_reindex_node_search($comment->getCommentedEntityId());
- }
- }
- function node_config_translation_info_alter(&$info) {
- $info['node_type']['class'] = 'Drupal\node\ConfigTranslation\NodeTypeMapper';
- }
|