0

I want to calculate the path aliases for array of nids. But it always give me the path for latest content. Actually I can not get all the nids. My array structure is like below. When I used the command product_childnid = array_pop(array_keys($result['node'])); I always get the 100 nid but I want all of them for path aliases.

Array
(
[node] => Array
    (
        [78] => stdClass Object
            (
                [nid] => 78
                [vid] => 78
                [type] => product
            )

        [80] => stdClass Object
            (
                [nid] => 80
                [vid] => 80
                [type] => product
            )

        [82] => stdClass Object
            (
                [nid] => 82
                [vid] => 82
                [type] => product
            )

        [84] => stdClass Object
            (
                [nid] => 84
                [vid] => 84
                [type] => product
            )

        [86] => stdClass Object
            (
                [nid] => 86
                [vid] => 86
                [type] => product
            )

        [88] => stdClass Object
            (
                [nid] => 88
                [vid] => 88
                [type] => product
            )

        [90] => stdClass Object
            (
                [nid] => 90
                [vid] => 90
                [type] => product
            )

        [92] => stdClass Object
            (
                [nid] => 92
                [vid] => 92
                [type] => product
            )

        [94] => stdClass Object
            (
                [nid] => 94
                [vid] => 94
                [type] => product
            )

        [100] => stdClass Object
            (
                [nid] => 100
                [vid] => 100
                [type] => product
              )
          )
  )

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
$query->entityCondition('bundle', 'product');
$query->propertyCondition('status', 1);
$query->fieldCondition('field_product_sub_cat', 'tid',array($leaf_nodes));
$result = $query->execute();
if (isset($result['node']))
{
  $product_childnid = array_pop(array_keys($result['node']));
}
$path = 'node/'.$product_childnid;
$product_alias = drupal_get_path_alias($path);

1 Answer 1

0

You have to iterate over all nodes in $result to do that. array_pop() only returns the last item in $result. So you'll have to do something like this:

$paths = array();
if (isset($result['node']))
{
  foreach($result['node'] as $key => $node) {
    $paths[$node->nid] = drupal_get_path_alias("node/$node->nid");
  }
}

After this $paths will contain an array of keys that map to each node id with a value that corresponds to its alias path.

4
  • Thanku @Shawn Conn, now I am getting all my paths but can you please explain me how can I represent this in my anchor tag?because I place it like this <a href="<?php print ($paths[$node->nid]); ?>"> but it gives me the last path
    – nomeer
    Commented Apr 13, 2015 at 3:26
  • Again, you have to iterate over the data. $paths will contain all elements because it is an array. You need to used a foreach to loop through the data.
    – Shawn Conn
    Commented Apr 13, 2015 at 4:22
  • FYI: You can save yourself a lot of trouble using the echo l("TEXT", $paths[$node->nid]); to print links.
    – Shawn Conn
    Commented Apr 13, 2015 at 4:22
  • $title = field_get_items('node',node_load($nid, NULL, TRUE), 'title'); and thenprint $title[0]['value']; I have to link over this, is this possible with l function?
    – nomeer
    Commented Apr 13, 2015 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.