Elgg\PluginHooksService::trigger PHP Méthode

trigger() public méthode

Triggers a plugin hook
See also: elgg_trigger_plugin_hook
public trigger ( $hook, $type, $params = null, $returnvalue = null )
    public function trigger($hook, $type, $params = null, $returnvalue = null)
    {
        $hooks = $this->getOrderedHandlers($hook, $type);
        foreach ($hooks as $callback) {
            if (!is_callable($callback)) {
                if ($this->logger) {
                    $inspector = new Inspector();
                    $this->logger->warn("handler for plugin hook [{$hook}, {$type}] is not callable: " . $inspector->describeCallable($callback));
                }
                continue;
            }
            $exit_warning = function () use($hook, $type, $callback) {
                $inspector = new Inspector();
                elgg_deprecated_notice("'{$hook}', '{$type}' plugin hook should not be used to serve a response. Instead return an " . "appropriate ResponseBuilder instance from an action or page handler. Do not terminate " . "code execution with exit() or die() in {$inspector->describeCallable($callback)}", '2.3');
            };
            if (in_array($hook, ['forward', 'action', 'route'])) {
                _elgg_services()->events->registerHandler('shutdown', 'system', $exit_warning);
            }
            $args = array($hook, $type, $returnvalue, $params);
            $temp_return_value = call_user_func_array($callback, $args);
            if (!is_null($temp_return_value)) {
                $returnvalue = $temp_return_value;
            }
            if (in_array($hook, ['forward', 'action', 'route'])) {
                _elgg_services()->events->unregisterHandler('shutdown', 'system', $exit_warning);
            }
        }
        return $returnvalue;
    }

Usage Example

Exemple #1
0
 /**
  * Process logging data
  *
  * @param mixed $data    The data to process
  * @param bool  $display Whether to display the data to the user. Otherwise log it.
  * @param int   $level   The logging level for this data
  * @return void
  */
 protected function process($data, $display, $level)
 {
     // plugin can return false to stop the default logging method
     $params = array('level' => $level, 'msg' => $data, 'display' => $display, 'to_screen' => $display);
     if (!$this->hooks->trigger('debug', 'log', $params, true)) {
         return;
     }
     // Do not want to write to screen before page creation has started.
     // This is not fool-proof but probably fixes 95% of the cases when logging
     // results in data sent to the browser before the page is begun.
     if (!isset($this->CONFIG->pagesetupdone)) {
         $display = false;
     }
     // Do not want to write to JS or CSS pages
     if (elgg_in_context('js') || elgg_in_context('css')) {
         $display = false;
     }
     if ($display == true) {
         echo '<pre class="elgg-logger-data">';
         echo htmlspecialchars(print_r($data, true), ENT_QUOTES, 'UTF-8');
         echo '</pre>';
     } else {
         error_log(print_r($data, true));
     }
 }
All Usage Examples Of Elgg\PluginHooksService::trigger
PluginHooksService