Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何正确使用Drupal 8中的hook_insert_Php_Drupal_Hook_Drupal Modules_Drupal 8 - Fatal编程技术网

Php 如何正确使用Drupal 8中的hook_insert

Php 如何正确使用Drupal 8中的hook_insert,php,drupal,hook,drupal-modules,drupal-8,Php,Drupal,Hook,Drupal Modules,Drupal 8,我为Drupal8创建了一个自定义模块。我制作了一个钩子,可以在创建新节点时进行检测,然后向订户发送通知。我的代码是: <?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 *

我为Drupal8创建了一个自定义模块。我制作了一个钩子,可以在创建新节点时进行检测,然后向订户发送通知。我的代码是:

<?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

您可能想要实现hook\u node\u presave

在将节点保存到数据库之前,它作用于正在创建或更新的节点:

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

@请查看下面的代码,并根据您的要求添加代码

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 
    }
  }     
} 

在实体保存到数据库后调用,这就是
isNew()
检查失败的原因。另一方面,您无需在
hook\u node\u insert'中检查该条件,因为该钩子仅在第一次将实体保存到数据库后执行。所以每次在
hook\u node\u insert中都可以确定这是一个新创建的节点。在创建节点时是否有一个@coderodour在将节点保存到数据库之前,它将是一个理想的地方,可以让所有值都可以进行操作。看起来drupal 8已经不推荐使用它了@coderodour是另一个选择,看看它是否适合您的需要。谢谢。出于某种原因,当放置一些测试代码时,例如
echo“console.log('it works');”它没有work@MikeL5799PHP的输出是缓冲的。您的
echo
实际上在这里工作,您不会在输出中看到它,因为在处理请求的脚本的
echo
之后的某个时刻,缓冲区被重置(说“好吧,让我放弃目前打印的内容,从头开始一个全新的空白页yeeeeh!!”)。因此,您得到的输出是在某些操作有效后生成的HTML。这种情况也会发生,因为重定向@请参阅。@MikeL5799请改用。如果你认为答案没问题,就接受吧!