0

I am planning to make a module whose controller is called or method in that controller executes when a node is published. The module should be able to get the title and other properties of article that is set to published. Here is controller:

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;

class myController extends ControllerBase
{
    //Function that should be executed when a specific node is published
    public function publish()
    {
     //here I should get title and other properties of the published node
    }
} 

I am new to drupal, I have looked into web and found that the solution is related to hooks but I don't know how to use them. Any help regarding the issue will be appreciated.

3
  • Welcome to Drupal Answers! What would publish() exactly do, after getting those node properties?
    – avpaderno
    Commented Feb 17, 2018 at 19:29
  • @kiamlaluno it will use them to call an api
    – Haris Khan
    Commented Feb 17, 2018 at 20:44
  • @kiamlaluno I am now working with hooks using the hook_node_presave() I am not able to get body of node (the content part of article and tags) is there a way to do this?
    – Haris Khan
    Commented Feb 17, 2018 at 20:46

1 Answer 1

0

The purpose of controllers is just providing output for a route, not implementing hooks. If you need to write code that is invoked when a node is published, you need to implement one of the following hooks:

hook_node_presave(), hook_node_update(), and hook_node_insert() get a Node object as parameter, so obtaining the body value is pretty easy.

use Drupal\node\Entity\Node;

function mymodule_node_presave(Node $node) {
  $body = $node->get('body')->value;
  $summary = $node->get('body')->summary;
}

Getting the taxonomy term from a reference field of a node can be achieved with code similar to the following one.

use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;

function mymodule_node_presave(Node $node) {
  $term = Term::load($node->get('field_tags')->target_id);
}

To that, you need to add the code to verify the node has been published.

use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;

function mymodule_node_presave(Node $node) {
  if ($node->status()) {
    // The node has been published.
    $body = $node->get('body')->value;
    $summary = $node->get('body')->summary;
    $term = Term::load($node->get('field_tags')->target_id);
    $term_name = $term->getName();
  }
}

If the taxonomy reference field is not field_tags, replace field_tags with the right machine name of the field.

5
  • Thank you for you response. $body = $node->get('body')->value; gets title of node and $summary = $node->get('body')->summary; gets content of node?
    – Haris Khan
    Commented Feb 18, 2018 at 10:32
  • No, both get the node body; the first gets the full body, while the second gets the trimmed body. For the node title, there is $node->getTitle().
    – avpaderno
    Commented Feb 18, 2018 at 10:35
  • $term_name = $term->getName(); provides only first tag. What if there are multiple tags to the article. @kiamlaluno
    – Haris Khan
    Commented Feb 18, 2018 at 12:11
  • $term_name = $term->getName(); provides only first tag. What if there are multiple tags to the article. @kiamlaluno
    – Haris Khan
    Commented Feb 18, 2018 at 12:11
  • @HarisKhan That is a different, new question. I think it was already asked, but if there isn't a question like that, ask it.
    – avpaderno
    Commented Feb 18, 2018 at 12:25

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.