View source
<?php
define('DEVEL_ERROR_HANDLER_NONE', 0);
define('DEVEL_ERROR_HANDLER_STANDARD', 1);
define('DEVEL_ERROR_HANDLER_BACKTRACE_KINT', 2);
define('DEVEL_ERROR_HANDLER_BACKTRACE_DPM', 4);
define('DEVEL_MIN_TEXTAREA', 50);
use Drupal\comment\CommentInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Utility\Error;
use Drupal\devel\EntityTypeInfo;
use Drupal\devel\ToolbarHandler;
use Drupal\Core\StringTranslation\TranslatableMarkup;
function devel_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.devel':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Devel module provides a suite of modules containing fun for module developers and themers. For more information, see the <a href=":url">online documentation for the Devel module</a>.', [
':url' => 'https://www.drupal.org/docs/8/modules/devel',
]) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Inspecting Service Container') . '</dt>';
$output .= '<dd>' . t('The module allows you to inspect Services and Parameters registered in the Service Container. You can see those informations on <a href=":url">Container info</a> page.', [
':url' => Url::fromRoute('devel.container_info.service')
->toString(),
]) . '</dd>';
$output .= '<dt>' . t('Inspecting Routes') . '</dt>';
$output .= '<dd>' . t('The module allows you to inspect routes information, gathering all routing data from <em>.routing.yml</em> files and from classes which subscribe to the route build/alter events. You can see those informations on <a href=":url">Routes info</a> page.', [
':url' => Url::fromRoute('devel.route_info')
->toString(),
]) . '</dd>';
$output .= '<dt>' . t('Inspecting Events') . '</dt>';
$output .= '<dd>' . t('The module allow you to inspect listeners registered in the event dispatcher. You can see those informations on <a href=":url">Events info</a> page.', [
':url' => Url::fromRoute('devel.event_info')
->toString(),
]) . '</dd>';
$output .= '</dl>';
return $output;
case 'devel.container_info.service':
case 'devel.container_info.parameter':
$output = '';
$output .= '<p>' . t('Displays Services and Parameters registered in the Service Container. For more informations on the Service Container, see the <a href=":url">Symfony online documentation</a>.', [
':url' => 'http://symfony.com/doc/current/service_container.html',
]) . '</p>';
return $output;
case 'devel.route_info':
$output = '';
$output .= '<p>' . t('Displays registered routes for the site. For a complete overview of the routing system, see the <a href=":url">online documentation</a>.', [
':url' => 'https://www.drupal.org/docs/8/api/routing-system',
]) . '</p>';
return $output;
case 'devel.event_info':
$output = '';
$output .= '<p>' . t('Displays events and listeners registered in the event dispatcher. For a complete overview of the event system, see the <a href=":url">Symfony online documentation</a>.', [
':url' => 'http://symfony.com/doc/current/components/event_dispatcher.html',
]) . '</p>';
return $output;
case 'devel.reinstall':
$output = '<p>' . t('<strong>Warning</strong> - will delete your module tables and configuration.') . '</p>';
$output .= '<p>' . t('Uninstall and then install the selected modules. <code>hook_uninstall()</code> and <code>hook_install()</code> will be executed and the schema version number will be set to the most recent update number.') . '</p>';
return $output;
case 'devel/session':
return '<p>' . t('Here are the contents of your <code>$_SESSION</code> variable.') . '</p>';
case 'devel.state_system_page':
return '<p>' . t('This is a list of state variables and their values. For more information read online documentation of <a href=":documentation">State API in Drupal 8</a>.', array(
':documentation' => "https://www.drupal.org/developing/api/8/state",
)) . '</p>';
case 'devel.layout_info':
$output = '';
$output .= '<p>' . t('Displays layouts available to the site. For a complete overview of the layout system, see the <a href=":url">Layout API documentation</a>.', [
':url' => 'https://www.drupal.org/docs/8/api/layout-api',
]) . '</p>';
return $output;
}
}
function devel_entity_type_alter(array &$entity_types) {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityTypeAlter($entity_types);
}
function devel_entity_operation(EntityInterface $entity) {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(EntityTypeInfo::class)
->entityOperation($entity);
}
function devel_toolbar() {
return \Drupal::service('class_resolver')
->getInstanceFromDefinition(ToolbarHandler::class)
->toolbar();
}
function devel_menu_links_discovered_alter(&$links) {
if (\Drupal::moduleHandler()
->moduleExists('layout_discovery')) {
$links['devel.layout_info'] = [
'title' => new TranslatableMarkup('Layouts Info'),
'route_name' => 'devel.layout_info',
'description' => new TranslatableMarkup('Overview of layouts available to the site.'),
'menu_name' => 'devel',
];
}
}
function devel_local_tasks_alter(&$local_tasks) {
if (\Drupal::moduleHandler()
->moduleExists('toolbar')) {
$local_tasks['devel.toolbar.settings_form'] = [
'title' => 'Toolbar Settings',
'base_route' => 'devel.admin_settings',
'route_name' => 'devel.toolbar.settings_form',
'class' => LocalTaskDefault::class,
'options' => [],
];
}
}
function devel_set_message($msg, $type = NULL) {
if (function_exists('drush_log')) {
drush_log($msg, $type);
}
else {
drupal_set_message($msg, $type, TRUE);
}
}
function devel_get_handlers() {
$error_handlers = \Drupal::config('devel.settings')
->get('error_handlers');
if (!empty($error_handlers)) {
unset($error_handlers[DEVEL_ERROR_HANDLER_NONE]);
}
return $error_handlers;
}
function devel_set_handler($handlers) {
if (empty($handlers)) {
restore_error_handler();
}
elseif (count($handlers) == 1 && isset($handlers[DEVEL_ERROR_HANDLER_STANDARD])) {
}
else {
set_error_handler('backtrace_error_handler');
}
}
function backtrace_error_handler($error_level, $message, $filename, $line, $context) {
if (!\Drupal::currentUser()
->hasPermission('access devel information')) {
if ($error_level & error_reporting()) {
$types = drupal_error_levels();
list($severity_msg, $severity_level) = $types[$error_level];
$backtrace = debug_backtrace();
$caller = Error::getLastCaller($backtrace);
_drupal_log_error(array(
'%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
'@message' => $message,
'%function' => $caller['function'],
'%file' => $caller['file'],
'%line' => $caller['line'],
'severity_level' => $severity_level,
'backtrace' => $backtrace,
), $error_level == E_RECOVERABLE_ERROR);
}
return;
}
if (error_reporting() == 0) {
return;
}
if (preg_match('#Cannot modify header information - headers already sent by \\([^\\)]*[/\\\\]devel[/\\\\]#', $message)) {
return;
}
if ($error_level & error_reporting()) {
if ($error_level == E_NOTICE) {
static $written = array();
if (!empty($written[$line][$filename][$message])) {
return;
}
$written[$line][$filename][$message] = TRUE;
}
$types = drupal_error_levels();
list($severity_msg, $severity_level) = $types[$error_level];
$backtrace = debug_backtrace();
$caller = Error::getLastCaller($backtrace);
$variables = array(
'%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
'@message' => $message,
'%function' => $caller['function'],
'%file' => $caller['file'],
'%line' => $caller['line'],
);
$msg = t('%type: @message in %function (line %line of %file).', $variables);
if (\Drupal::config('system.logging')
->get('error_level') != 'hide') {
$error_handlers = devel_get_handlers();
if (!empty($error_handlers[DEVEL_ERROR_HANDLER_STANDARD])) {
drupal_set_message($msg, $severity_level <= RfcLogLevel::NOTICE ? 'error' : 'warning', TRUE);
}
if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_KINT])) {
print kpr(ddebug_backtrace(TRUE, 1), TRUE, $msg);
}
if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_DPM])) {
dpm(ddebug_backtrace(TRUE, 1), $msg, 'warning');
}
}
\Drupal::logger('php')
->log($severity_level, $msg);
}
}
function devel_page_attachments_alter(&$page) {
if (\Drupal::currentUser()
->hasPermission('access devel information') && \Drupal::config('devel.settings')
->get('page_alter')) {
dpm($page, 'page');
}
}
function dfb() {
$args = func_get_args();
\Drupal::service('devel.dumper')
->dump($args, NULL, 'firephp');
}
function dfbt($label) {
\Drupal::service('devel.dumper')
->dump(FirePHP::TRACE, $label, 'firephp');
}
function dcp() {
$args = func_get_args();
\Drupal::service('devel.dumper')
->dump($args, NULL, 'chromephp');
}
if (!function_exists('dd')) {
function dd($data, $label = NULL) {
return \Drupal::service('devel.dumper')
->debug($data, $label, 'default');
}
}
if (!function_exists('ddm')) {
function ddm($data, $label = NULL) {
return \Drupal::service('devel.dumper')
->debug($data, $label, 'default');
}
}
function dpm($input, $name = NULL, $type = 'status') {
\Drupal::service('devel.dumper')
->message($input, $name, $type);
return $input;
}
function dvm($input, $name = NULL) {
\Drupal::service('devel.dumper')
->message($input, $name, 'status', 'drupal_variable');
return $input;
}
function dsm($input, $name = NULL) {
return dpm($input, $name);
}
function dpr($input, $export = FALSE, $name = NULL) {
return \Drupal::service('devel.dumper')
->dumpOrExport($input, $name, $export, 'default');
}
function kpr($input, $export = FALSE, $name = NULL) {
return \Drupal::service('devel.dumper')
->dumpOrExport($input, $name, $export);
}
function dvr($input, $export = FALSE, $name = NULL) {
return \Drupal::service('devel.dumper')
->dumpOrExport($input, $name, $export, 'drupal_variable');
}
function dargs($always = TRUE) {
static $printed;
if ($always || !$printed) {
$bt = debug_backtrace();
print kpr($bt[1]['args'], TRUE);
$printed = TRUE;
}
}
function dpq($query, $return = FALSE, $name = NULL) {
if (\Drupal::currentUser()
->hasPermission('access devel information')) {
if (method_exists($query, 'preExecute')) {
$query
->preExecute();
}
$sql = (string) $query;
$quoted = array();
$connection = Database::getConnection();
foreach ((array) $query
->arguments() as $key => $val) {
$quoted[$key] = is_null($val) ? 'NULL' : $connection
->quote($val);
}
$sql = strtr($sql, $quoted);
if ($return) {
return $sql;
}
dpm($sql, $name);
}
return $return ? NULL : $query;
}
function devel_render() {
$args = func_get_args();
kpr(end($args));
return reset($args);
}
function ddebug_backtrace($return = FALSE, $pop = 0, $options = DEBUG_BACKTRACE_PROVIDE_OBJECT) {
if (\Drupal::currentUser()
->hasPermission('access devel information')) {
$backtrace = debug_backtrace($options);
while ($pop-- > 0) {
array_shift($backtrace);
}
$counter = count($backtrace);
$path = $backtrace[$counter - 1]['file'];
$path = substr($path, 0, strlen($path) - 10);
$paths[$path] = strlen($path) + 1;
$paths[DRUPAL_ROOT] = strlen(DRUPAL_ROOT) + 1;
$nbsp = " ";
if (\Drupal::config('system.logging')
->get('error_level') != 'hide') {
while (!empty($backtrace)) {
$call = array();
if (isset($backtrace[0]['file'])) {
$call['file'] = $backtrace[0]['file'];
foreach ($paths as $path => $len) {
if (strpos($backtrace[0]['file'], $path) === 0) {
$call['file'] = substr($backtrace[0]['file'], $len);
}
}
$call['file'] .= ':' . $backtrace[0]['line'];
}
if (isset($backtrace[1])) {
if (isset($backtrace[1]['class'])) {
$function = $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function'] . '()';
}
else {
$function = $backtrace[1]['function'] . '()';
}
$backtrace[1] += array(
'args' => array(),
);
foreach ($backtrace[1]['args'] as $key => $value) {
$call['args'][$key] = $value;
}
}
else {
$function = 'main()';
$call['args'] = $_GET;
}
$nicetrace[($counter <= 10 ? $nbsp : '') . --$counter . ': ' . $function] = $call;
array_shift($backtrace);
}
if ($return) {
return $nicetrace;
}
kpr($nicetrace);
}
}
}
function devel_form_user_admin_permissions_alter(&$form, FormStateInterface $form_state) {
if (\Drupal::currentUser()
->hasPermission('access devel information') && \Drupal::config('devel.settings')
->get('raw_names')) {
foreach (Element::children($form['permissions']) as $key) {
if (isset($form['permissions'][$key][0])) {
$form['permissions'][$key][0]['#wrapper_attributes']['title'] = $key;
}
elseif (isset($form['permissions'][$key]['description'])) {
$form['permissions'][$key]['description']['#wrapper_attributes']['title'] = $key;
}
}
}
}
function devel_form_system_modules_alter(&$form, FormStateInterface $form_state) {
if (\Drupal::currentUser()
->hasPermission('access devel information') && \Drupal::config('devel.settings')
->get('raw_names', FALSE) && isset($form['modules']) && is_array($form['modules'])) {
foreach (Element::children($form['modules']) as $group) {
if (is_array($form['modules'][$group])) {
foreach (Element::children($form['modules'][$group]) as $key) {
if (isset($form['modules'][$group][$key]['name']['#markup'])) {
$form['modules'][$group][$key]['name']['#markup'] = '<span title="' . $key . '">' . $form['modules'][$group][$key]['name']['#markup'] . '</span>';
}
}
}
}
}
}
function devel_query_debug_alter(AlterableInterface $query) {
if (!$query
->hasTag('debug-semaphore')) {
$query
->addTag('debug-semaphore');
dpq($query);
}
}