Skip to content

Instantly share code, notes, and snippets.

@laceysanderson
Created December 4, 2023 22:04
Show Gist options
  • Save laceysanderson/467bf20b6d5bd191f978419b728b8d14 to your computer and use it in GitHub Desktop.
Save laceysanderson/467bf20b6d5bd191f978419b728b8d14 to your computer and use it in GitHub Desktop.
Playing with Tripal entities in drush php:cli
<?php
/**
* These commands are expected to be used in the drush php:cli
* but could likely be wrapped into a drush script or used elsewhere.
*/
// Load all Tripal Content Entities of type gene.
$entities = \Drupal::entityTypeManager()->getStorage('tripal_entity')->loadByProperties(['type' => 'gene']);
// Save a specific entity for use later.
// When run in drush php:cli this command also prints a nice summary of all field values for this entity ;-p
// See \Drupal\Core\Entity\ContentEntityBase for methods you can use on $entity.
// https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21ContentEntityBase.php/class/ContentEntityBase/10
$entity = $entities[53];
// Get a list of all fields on this entity and their field definitions.
$field_definitions = $entity->getFieldDefinitions();
// Collect all our details into an array for later.
$yaml_array = [];
// Start working on a specific field...
$field_name = 'gene_type';
$field_defn = $field_definitions[$field_name];
// -- Add it to the yaml
$yaml_array[$field_name] = [
'field_name' => $field_name,
];
// Get the storage Settings
$storage_settings = $field_defn->getSetting('storage_plugin_settings');
// Use the storage settings to set the base table in the yaml.
$yaml_array[$field_name]['base_table'] = $storage_settings['base_table'];
// Get ready to process properties.
$yaml_array[$field_name]['properties'] = [];
// Get the property type objects.
$properties = $entity->{$field_name}->first()->tripalTypes($field_defn);
// For each object...
foreach ($properties as $propertyType) {
$property_key = $propertyType->getKey();
$yaml_array[$field_name]['properties'][$property_key] = $propertyType->getStorageSettings();
$yaml_array[$field_name]['properties'][$property_key]['propertyType class'] = get_class($propertyType);
}
// Just mentioning a variable on the drush php:cli will print it's contents.
$yaml_array[$field_name]['properties'];
// Print it as YAML.
\Drupal\Component\Serialization\Yaml::encode($yaml_array);
@guignonv
Copy link

guignonv commented Dec 5, 2023

Good hints! :) Thanks Lacey!

I'd like to add a personal opinion (but shared by others ;) ):
\Drupal::entityTypeManager() is just a shorthand to \Drupal::service('entity_type.manager')
so, while it's definitely ok to use it in php:cli (and I would as well), prefer the use of \Drupal::service('entity_type.manager') in your developments. You avoid an intermediary call and there are other several advantages (while the chances the service name changes is lower than its methods you may use in your code do)!

Also some more code that might be useful to developers:

// Another way to access field definitions (the second 'tripal_entity' should be replaced by the bundle name if one):
$field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('tripal_entity', 'tripal_entity');

// Clear cached definitions when changing things:
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment