0

I am creating a Drupal Dashboard (PHP 7.2, Drupal 8.9). I'm trying to query the node table using the following code.

$database = \Drupal::database();

$query = $database->query("SELECT field_advertiser_id_value FROM {node__field_advertiser_id} WHERE entity_id = :entity_id", [
    ':entity_id' => $entity_id
]);

$result = $query->execute();
print_r($result);

I am not sure why $query->execute() returns false. This table exists in the l database.

The database contains both the node and the node__field_advertiser_id tables.

enter image description here

Inserting data in the node__field_advertiser_id works correctly.

$database = \Drupal::database();

$database->insert('node__field_advertiser_id')
    ->fields([
      'entity_id' => $advertiser_nid,
      'revision_id' => 1,
      'delta' => 0,
      'field_advertiser_id_value' => $advertiser_id
    ])
    ->execute();

I am trying to fetch details from the node__field_advertiser_id from hook_node_delete(). The data is still there in the table when I try to run the SQL query.

It would be great if anyone can help me out with this.

0

1 Answer 1

2

I don't think you need an execute on a regular query like that. But the way to get any node data would be loading the node with the entityTypeManager service, and getting the field values with $node->get('field_advertiser_id')->getValue(). Its rare to have to get a value that way from the database.

The same goes for the insert or update - you definitely should be using the API for this and not direct database writes.

On top of that you don't need to do anything with the database at all in this example, because the node being deleted is already passed to this hook. Just look at its field values. https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_delete/9.1.x

But in a case like this, hook_entity_delete may be too late to get values depending on what you need to do. Instead, you probably want to use hook_entity_predelete: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_predelete/9.1.x

Example:

function mymodule_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity instanceof NodeInterface && $entity->getType() == 'your_content_type') {
    $advertiser_id = $entity->get('field_advertiser_id')->first()->getValue();
    
    // do something with id, like load that node
    if (isset($advertiser_id)) {
      $entityTypeManager = \Drupal::entityTypeManager();
      $advertiser = $entityTypeManager->getStorage('node')->load($advertiser_id);
      
      if ($advertiser instanceof NodeInterface && $entity->getType() == 'advertiser_content_type') {
        // Delete this other node.
        $advertiser->delete();
      }
    }
  }
}

You have to be careful with these hooks though, because it can trigger an infinite loop since these hooks are fired every time the actions are performed. Meaning, if you wanted to delete the advertiser node here for example, this hook would get called on that action too. So you need to be specific with your conditions check each value and logic. People get caught in presave hooks all the time.

3
  • Thanks @Kevin I tried to get value like this - $node->get('field_advertiser_id')->getValue() but I didn't got it. Actually, the field_advertiser_id is a disabled field in my content type 'Advertiser'. I kept this field to keep track of ids in the Drupal database. I have a Postgres DB where I am storing advertisers and returning id back to Drupal database and storing it in the node__field_advertiser_id table. Hence, I am using a SQL query to access the node table. :)
    – Harshit
    Oct 31, 2020 at 6:38
  • None of that matters to the API. You don't need to use sql to access the data and it does not matter if the database is Postgres. Otherwise this question does not make a lot of sense because it is not described correctly.
    – Kevin
    Oct 31, 2020 at 13:26
  • I got you now :) Thanks, Kevin for the detailed answer. It took some time for me to understand your answer since I am new to Drupal. For my case, hook_entity_predelete works like charm!
    – Harshit
    Nov 2, 2020 at 5:11

Your Answer

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

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