В моей функции предварительной обработки у меня есть текущий идентификатор узла и массив со всеми идентификаторами узла для типа содержимого, называемого recipes:
$current_node_id = \Drupal::routeMatch()->getParameter('node');
$nids = \Drupal::entityQuery('node')->condition('type','recipes')->execute();
$recipes_id = \Drupal\node\Entity\Node::loadMultiple($nids);
Результат $ recipes_id следующий на рисунке:
![enter image description here](https://i.stack.imgur.com/sfjZd.png)
Далее я хочу l oop через все идентификаторы и получить идентификатор, равный текущему идентификатору:
$i = 0;
foreach($recipes_id as $key => $value) {
if($key == $current_node_id->id()) {
}
$i++;
}
В операторе if я хочу получить доступ к идентификатору, который находится после переменной $ key. поэтому, если текущий идентификатор равен 150, я должен получить доступ к 159 и т. д. c. Для этого в операторе if я добавил следующее:
$variables['node'] = $recipes_id[$i];
Когда я просматриваю страницу, я вижу ноль:
{{ kint(node) }}
Вот вся моя функция:
function amarula_preprocess_page(&$variables) {
/**
* get current node id
*/
$current_node_id = \Drupal::routeMatch()->getParameter('node');
/**
* check the current language
*/
$current_lang = \Drupal::languageManager()->getCurrentLanguage()->getId();
$variables['current_lang'] = $current_lang; //current language code
/**
* get all recipes node id
*/
$nids = \Drupal::entityQuery('node')->condition('type','recipes')->execute();
$recipes_id = \Drupal\node\Entity\Node::loadMultiple($nids);
$variables['recipes_id'] = $recipes_id; // recipes node ID
$i = 0;
foreach($recipes_id as $key => $value)
{
if($key == $current_node_id->id())
{
$variables['node'] = $recipes_id[$i + 1];
break;
}
$i++;
}
}
Имея текущий идентификатор, как я могу найти идентификатор после этого в массиве?