How to get nid and title from a node array










0















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 '18 at 8:47















0















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 '18 at 8:47













0












0








0








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 '18 at 13:16









leymannx

7,47453063




7,47453063










asked Nov 15 '18 at 8:38









harshalharshal

3,17942656




3,17942656







  • 1





    A node is an object, not an array. Learn how to use OOP

    – Hudri
    Nov 15 '18 at 8:47












  • 1





    A node is an object, not an array. Learn how to use OOP

    – Hudri
    Nov 15 '18 at 8:47







1




1





A node is an object, not an array. Learn how to use OOP

– Hudri
Nov 15 '18 at 8:47





A node is an object, not an array. Learn how to use OOP

– Hudri
Nov 15 '18 at 8:47










3 Answers
3






active

oldest

votes


















4














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 '18 at 10:36


















1














you can get the node id like this :



$nid = $node->id();


and for the title :



$title = $node->label();





share|improve this answer






























    0














    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',
      autoActivateHeartbeat: false,
      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









      4














      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 '18 at 10:36















      4














      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 '18 at 10:36













      4












      4








      4







      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 '18 at 13:31









      leymannx

      7,47453063




      7,47453063










      answered Nov 15 '18 at 8:59









      berramouberramou

      2,7012312




      2,7012312







      • 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 '18 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 '18 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 '18 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 '18 at 10:36













      1














      you can get the node id like this :



      $nid = $node->id();


      and for the title :



      $title = $node->label();





      share|improve this answer



























        1














        you can get the node id like this :



        $nid = $node->id();


        and for the title :



        $title = $node->label();





        share|improve this answer

























          1












          1








          1







          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 '18 at 9:02









          izusizus

          640414




          640414





















              0














              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



























                0














                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

























                  0












                  0








                  0







                  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 '18 at 14:42









                  Gnanaguru MariyadassGnanaguru 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.




                      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







                      這個網誌中的熱門文章

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

                      Museum of Modern and Contemporary Art of Trento and Rovereto