How to get nid and title from a node array





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
0
down vote

favorite












use DrupalnodeEntityNode;

function abc($v) {
$node = DrupalnodeEntityNode::load($v);
print_r($node);
}


The array structure of $node is as given below




DrupalnodeEntityNode Object (
[in_preview] =>

[values:protected] => Array
(


[nid] => Array
(
[x-default] => 166
)

[vid] => Array
(
[x-default] => 180
)

[type] => Array
(
[x-default] => sections
)

[uuid] => Array
(
[x-default] => 7759efc9-cfff-4d58-b392-8d60b9903323
)

[langcode] => Array
(
[x-default] => en
)










share|improve this question




















  • 1




    A node is an object, not an array. Learn how to use OOP
    – Hudri
    Nov 15 at 8:47

















up vote
0
down vote

favorite












use DrupalnodeEntityNode;

function abc($v) {
$node = DrupalnodeEntityNode::load($v);
print_r($node);
}


The array structure of $node is as given below




DrupalnodeEntityNode Object (
[in_preview] =>

[values:protected] => Array
(


[nid] => Array
(
[x-default] => 166
)

[vid] => Array
(
[x-default] => 180
)

[type] => Array
(
[x-default] => sections
)

[uuid] => Array
(
[x-default] => 7759efc9-cfff-4d58-b392-8d60b9903323
)

[langcode] => Array
(
[x-default] => en
)










share|improve this question




















  • 1




    A node is an object, not an array. Learn how to use OOP
    – Hudri
    Nov 15 at 8:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











use DrupalnodeEntityNode;

function abc($v) {
$node = DrupalnodeEntityNode::load($v);
print_r($node);
}


The array structure of $node is as given below




DrupalnodeEntityNode Object (
[in_preview] =>

[values:protected] => Array
(


[nid] => Array
(
[x-default] => 166
)

[vid] => Array
(
[x-default] => 180
)

[type] => Array
(
[x-default] => sections
)

[uuid] => Array
(
[x-default] => 7759efc9-cfff-4d58-b392-8d60b9903323
)

[langcode] => Array
(
[x-default] => en
)










share|improve this question















use DrupalnodeEntityNode;

function abc($v) {
$node = DrupalnodeEntityNode::load($v);
print_r($node);
}


The array structure of $node is as given below




DrupalnodeEntityNode Object (
[in_preview] =>

[values:protected] => Array
(


[nid] => Array
(
[x-default] => 166
)

[vid] => Array
(
[x-default] => 180
)

[type] => Array
(
[x-default] => sections
)

[uuid] => Array
(
[x-default] => 7759efc9-cfff-4d58-b392-8d60b9903323
)

[langcode] => Array
(
[x-default] => en
)







8 nodes






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 13:16









leymannx

6,55942657




6,55942657










asked Nov 15 at 8:38









harshal

3,14742455




3,14742455








  • 1




    A node is an object, not an array. Learn how to use OOP
    – Hudri
    Nov 15 at 8:47














  • 1




    A node is an object, not an array. Learn how to use OOP
    – Hudri
    Nov 15 at 8:47








1




1




A node is an object, not an array. Learn how to use OOP
– Hudri
Nov 15 at 8:47




A node is an object, not an array. Learn how to use OOP
– Hudri
Nov 15 at 8:47










3 Answers
3






active

oldest

votes

















up vote
4
down vote













As mentioned in the comment of @Hudri Node is an object not an array.



In Drupal 8 there are two ways to get the value of a field.





  1. $node->field_name->value

  2. $node->get("field_name")->getValue()


In your case you can get the nid and title like below:



$nid = $node->id();
$title = $node->label();


or



$title = $node->getTitle();


To read about objected-oriented programming conventions In Drupal 8 check this blog article: Drupal 8 API: objected-oriented programming conventions.






share|improve this answer



















  • 4




    it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
    – Clive
    Nov 15 at 10:36


















up vote
1
down vote













you can get the node id like this :



$nid = $node->id();


and for the title :



$title = $node->label();





share|improve this answer




























    up vote
    0
    down vote













    You can get the node id and node title values by using Node::load function.



    use DrupalnodeEntityNode;



    function abc($v)
    {



    $node = Node::load($v);

    $strNodeId = $node->nid->value;

    $strNodeTitle = $node->title->value;

    echo 'Id: '.$strNodeId;

    echo 'Title: '.$strNodeTitle;
    exit();


    }






    share|improve this answer





















      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "220"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f272522%2fhow-to-get-nid-and-title-from-a-node-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      As mentioned in the comment of @Hudri Node is an object not an array.



      In Drupal 8 there are two ways to get the value of a field.





      1. $node->field_name->value

      2. $node->get("field_name")->getValue()


      In your case you can get the nid and title like below:



      $nid = $node->id();
      $title = $node->label();


      or



      $title = $node->getTitle();


      To read about objected-oriented programming conventions In Drupal 8 check this blog article: Drupal 8 API: objected-oriented programming conventions.






      share|improve this answer



















      • 4




        it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
        – Clive
        Nov 15 at 10:36















      up vote
      4
      down vote













      As mentioned in the comment of @Hudri Node is an object not an array.



      In Drupal 8 there are two ways to get the value of a field.





      1. $node->field_name->value

      2. $node->get("field_name")->getValue()


      In your case you can get the nid and title like below:



      $nid = $node->id();
      $title = $node->label();


      or



      $title = $node->getTitle();


      To read about objected-oriented programming conventions In Drupal 8 check this blog article: Drupal 8 API: objected-oriented programming conventions.






      share|improve this answer



















      • 4




        it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
        – Clive
        Nov 15 at 10:36













      up vote
      4
      down vote










      up vote
      4
      down vote









      As mentioned in the comment of @Hudri Node is an object not an array.



      In Drupal 8 there are two ways to get the value of a field.





      1. $node->field_name->value

      2. $node->get("field_name")->getValue()


      In your case you can get the nid and title like below:



      $nid = $node->id();
      $title = $node->label();


      or



      $title = $node->getTitle();


      To read about objected-oriented programming conventions In Drupal 8 check this blog article: Drupal 8 API: objected-oriented programming conventions.






      share|improve this answer














      As mentioned in the comment of @Hudri Node is an object not an array.



      In Drupal 8 there are two ways to get the value of a field.





      1. $node->field_name->value

      2. $node->get("field_name")->getValue()


      In your case you can get the nid and title like below:



      $nid = $node->id();
      $title = $node->label();


      or



      $title = $node->getTitle();


      To read about objected-oriented programming conventions In Drupal 8 check this blog article: Drupal 8 API: objected-oriented programming conventions.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 at 13:31









      leymannx

      6,55942657




      6,55942657










      answered Nov 15 at 8:59









      berramou

      1,526212




      1,526212








      • 4




        it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
        – Clive
        Nov 15 at 10:36














      • 4




        it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
        – Clive
        Nov 15 at 10:36








      4




      4




      it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
      – Clive
      Nov 15 at 10:36




      it's not ideal to use magic methods to get the title. There are two better methods, Node::getTitle() and Entity::label(), that should take precedence
      – Clive
      Nov 15 at 10:36












      up vote
      1
      down vote













      you can get the node id like this :



      $nid = $node->id();


      and for the title :



      $title = $node->label();





      share|improve this answer

























        up vote
        1
        down vote













        you can get the node id like this :



        $nid = $node->id();


        and for the title :



        $title = $node->label();





        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          you can get the node id like this :



          $nid = $node->id();


          and for the title :



          $title = $node->label();





          share|improve this answer












          you can get the node id like this :



          $nid = $node->id();


          and for the title :



          $title = $node->label();






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 at 9:02









          izus

          538213




          538213






















              up vote
              0
              down vote













              You can get the node id and node title values by using Node::load function.



              use DrupalnodeEntityNode;



              function abc($v)
              {



              $node = Node::load($v);

              $strNodeId = $node->nid->value;

              $strNodeTitle = $node->title->value;

              echo 'Id: '.$strNodeId;

              echo 'Title: '.$strNodeTitle;
              exit();


              }






              share|improve this answer

























                up vote
                0
                down vote













                You can get the node id and node title values by using Node::load function.



                use DrupalnodeEntityNode;



                function abc($v)
                {



                $node = Node::load($v);

                $strNodeId = $node->nid->value;

                $strNodeTitle = $node->title->value;

                echo 'Id: '.$strNodeId;

                echo 'Title: '.$strNodeTitle;
                exit();


                }






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  You can get the node id and node title values by using Node::load function.



                  use DrupalnodeEntityNode;



                  function abc($v)
                  {



                  $node = Node::load($v);

                  $strNodeId = $node->nid->value;

                  $strNodeTitle = $node->title->value;

                  echo 'Id: '.$strNodeId;

                  echo 'Title: '.$strNodeTitle;
                  exit();


                  }






                  share|improve this answer












                  You can get the node id and node title values by using Node::load function.



                  use DrupalnodeEntityNode;



                  function abc($v)
                  {



                  $node = Node::load($v);

                  $strNodeId = $node->nid->value;

                  $strNodeTitle = $node->title->value;

                  echo 'Id: '.$strNodeId;

                  echo 'Title: '.$strNodeTitle;
                  exit();


                  }







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 at 14:42









                  Gnanaguru Mariyadass

                  211




                  211






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Drupal Answers!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f272522%2fhow-to-get-nid-and-title-from-a-node-array%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?