Comment puis-je trouver la clé suivante d'un tableau de valeur de paire

agent utilisateur :

Dans ma fonction prétraiter, je l'identifiant du noeud courant, et un tableau avec tous les noeuds ids pour un type de contenu appelé recettes:

$current_node_id = \Drupal::routeMatch()->getParameter('node');
$nids = \Drupal::entityQuery('node')->condition('type','recipes')->execute();
$recipes_id =  \Drupal\node\Entity\Node::loadMultiple($nids);

Le résultat de $ recipes_id est la suivante dans l'image:

entrez la description d'image ici

Ensuite, je veux faire une boucle à travers toutes les cartes d'identité et obtenir l'ID qui est égal à l'identifiant courant:

 $i = 0;
 foreach($recipes_id as $key => $value) {
    if($key == $current_node_id->id()) {

    }
    $i++;
 }

Dans l'instruction if, je veux accéder à l'ID qui est la variable de la touche $. donc si l'identifiant courant est de 150 j'accéder à 159, etc. Pour cela, à l'intérieur de l'instruction if I ajouté ce qui suit:

$variables['node'] = $recipes_id[$i];

Quand je regarde la page si, je vois nulle:

{{ kint(node) }}

Voici toute ma fonction:

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++;
    }
}

Avoir l'ID actuel, comment puis-je trouver l'ID après dans le tableau?

Omar Abbas:

voici comment vous obtenez la clé suivante.

$keys = array_keys($recipes_id);
$i = 0;
foreach($keys as $value)
{
    if($value == $current_node_id->id()){
        $variables['node'] = $keys[$i + 1];
        break;
    }
    $i++;
}

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=294563&siteId=1
conseillé
Classement