You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Set this in settings.local.php so that it displays locally, but not on prod. The default Logging and errors should be 'None'.
// none - display none
// some - errors and warning
// all - all messages
// verbose - all messages with backtrace information
$config['system.logging']['error_level'] = 'all';
Set kint debug max level.
If kint is taking forever to load or crashing the page, try reducing the max level.
You will need to copy sites/example.settings.local.php to sites/default/settings.local.php (and ensure settings.php includes settings.local.php) or put this in your settings.php file: $settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';.
// This function exists in core/includes/bootstrap.inc.
// Just need to add lines 6-8 to it.
function _drupal_error_handler($error_level, $message, $filename, $line, $context) {
require_once DRUPAL_ROOT . '/includes/errors.inc';
require_once DRUPAL_ROOT . '/modules/contrib/devel/kint/kint.module';
$d = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
ksm($message, $d);
_drupal_error_handler_real($error_level, $message, $filename, $line, $context);
}
Debugging search API solr queries:
// You can output the Request object using kint/kpm, but it can be hard
// to figure out where to set the debugging code. The best place is in
// the executeRequest function in the following file:
// search_api_solr/src/SolrConnector/SolrConnectorPluginBase.php
Starting point for debugging ElasticSearch stuff, in the file
// src/ElasticSearch/Parameters/Builder/SearchBuilder.php:
// Add ksm at the end of build() and getSearchQueryOptions()
$node->field_example will return a FieldItemList object (or another class that extends FieldItemList
If you want to drill down to values under the field, you can add ->first() or leave it off and it'll automatically use the first item. This is true if the field is a single item or multi-values. Example: $node->field_example->target_id or $node->field_example->first()->target_id.
If the field is an entity reference, you can get the full entity: $node->field_example->entity (this gets the first entity if it's a multi-value).
On multi-value fields to get something other than the first you can use the array index: $node->field_example[1].
Entity reference
// Both of these return the same Entity ID value.
$node->field_ref->target_id;
$node->field_ref->first()->target_id;
// Get the full entity. "->entity" is only valid when the field is an entity reference.
$node->field_ref->entity;
// Get the value as an array: ['target_id' => '1'].
$node->field_ref->getValue();
// Will return null.
$node->field_ref->value;
URL
$node->field_url->uri;
$node->field_url->title;
$node->field_url->options; // Array of options.
$node->field_url->entity; // Returns null because it isn't an entity.
$node->field_url->getValue(); // Returns ['uri' => '', 'title' => '', 'options => []]
$node->field_url->value; // Returns null.
Text values
// Get the value as a string.
$node->field_text->value;
// Get the value as an array: ['value' => 'text'].
$node->field_text->getValue();
$file = \Drupal\file\Entity\File::load(1007);
OR
$file = \Drupal::entityTypeManager()->getStorage('file')->load(1007);
Working with file entities
// Get the URI (including wrapper, such as public://).
$uri = $file->getFileUri();
// Get the full URL path.
$url = file_create_url($file->getFileUri());
// Get relative path of the URL (w/o domain).
$path = file_url_transform_relative($url);
// Libraries is an array of the library data.
// Extension is 'core' or the module/theme that defined the libraries.
function hook_library_info_alter(&$libraries, $extension)
// Remove the prepareUpdate() section if you just want a normal import.
// To rollback just change 'import' to 'rollback'.
$migration = \Drupal::service('plugin.manager.migration')->createInstance('machine_name');
$migration->getIdMap()->prepareUpdate();
$executable = new \Drupal\migrate_tools\MigrateExecutable($migration, new \Drupal\migrate\MigrateMessage());
$executable->import();
// MigrateExecutable takes an optional third argument where you can provide options including
// limit to limit the number of migrations to perform, and idlist to only migrate certain source
// IDs.
// Example: ['limit' => 10, 'idlist' => '1,2,3']
// GET parameter bag.
$bag = \Drupal::request()->query;
// POST parameter bag.
$bag = \Drupal::request()->request;
// Get all parameters as array.
$bag->all();
// Get individual result.
$bag->get('name');
// Get count of parameters.
$bag->count();
Trigger select entities to be re-indexed through the Search API.
// This is for Title Record entities, but any entity will do.
use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
use Drupal\omega_hub\Entity\TitleRecord;
$entity_ids = [507863, 509240, 513703, 515100, 536124, 537058, 541569];
$combine_id = function ($entity_id) {
return $entity_id . ':und';
};
$update_ids = array_map($combine_id, $entity_ids);
$entity = TitleRecord::load(507863);
$indexes = ContentEntity::getIndexesForEntity($entity);
foreach ($indexes as $index) {
$index->trackItemsUpdated('entity:title_record', $update_ids);
}
// Get query object (only after executing the view).
$query = $view->query;
// Get view ID.
$view->id();
// Get current display.
$view->current_display();
// Get results (after execution).
$view->result;