简体   繁体   中英

How to properly use the hook_insert in Drupal 8

I created a custom module for Drupal 8. I made a hook that is to detect whenever a new node is created, then send a notification to a subscriber. The code I have is this:

<?php 

/**
* @file
* Contains onesignal_api.module.
* 
*/

use Drupal\Core\Entity\EntityInterface;

/***
* Hook into OneSignal API to send push notifications once a new node is created
*/

function onesignal_api_insert(\Drupal\Core\Entity\EntityInterface $node) {
if($node->isNew()) {
    function sendMessage() {
      $content      = array(
          "en" => 'New Node Created'
      );
      $hashes_array = array();
      array_push($hashes_array, array(
          "id" => "like-button",
          "text" => "Like",
          "icon" => "http://i.imgur.com/N8SN8ZS.png",
          "url" => "http://push-test/"
      ));
      array_push($hashes_array, array(
          "id" => "like-button-2",
          "text" => "Like2",
          "icon" => "http://i.imgur.com/N8SN8ZS.png",
          "url" => "http://push-test/"
      ));
      $fields = array(
          'app_id' => "XXXXXXXXX",
          'include_player_ids' => array("XXXXXX","XXXXX","XXXXXX"),
          'data' => array(
              "foo" => "bar"
          ),
          'contents' => $content,
          'web_buttons' => $hashes_array
      );

      $fields = json_encode($fields);
      print("\nJSON sent:\n");
      print($fields);

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Content-Type: application/json; charset=utf-8',
          'Authorization: Basic XXXXXXX'
      ));
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
      curl_setopt($ch, CURLOPT_HEADER, FALSE);
      curl_setopt($ch, CURLOPT_POST, TRUE);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

      $response = curl_exec($ch);
      curl_close($ch);

      return $response;
  }

  $response = sendMessage();
  $return["allresponses"] = $response;
  $return = json_encode($return);

  $data = json_decode($response, true);
  print_r($data);
  $id = $data['id'];
  print_r($id);

  print("\n\nJSON received:\n");
  print($return);
  print("\n");

 }//if Node is new
}//func hook signal

Is there something that I need to change to get this to work? all of the code inside of the if statement works outside of the if statement.

You probably want to implement hook_node_presave .

It acts on a node being created or updated before it is saved to the database :

function onesignal_api_node_presave(\Drupal\Core\Entity\EntityInterface $node) {
  if ($node->isNew()) {
    // $node is about to be created...
  }
}

@see hook_ENTITY_TYPE_presave

Please look below code and add your code as per your requirements.

function onesignal_api_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {

  if ($entity->getEntityType()->id() == 'node') {
    switch ($entity->bundle()) {

       case 'content-type-name':    // if multiple entities 
    }
  }     
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM