entities

my_module.schema.yml
views.filter.my_module_entity_options:
  type: views.filter.many_to_one
  label: 'Entity options'
EntityOptions.php
<?php

namespace Drupal\my_module\Plugin\views\filter;

use Drupal\views\Attribute\ViewsFilter;
use Drupal\views\Plugin\views\filter\ManyToOne;

/**
 * Filters a multi-value field by entities of a configurable entity type.
 */
#[ViewsFilter("my_module_entity_options")]
class EntityOptions extends ManyToOne {

  /**
   * {@inheritdoc}
   */
  public function getValueOptions() {
    if (!isset($this->valueOptions)) {
      $this->valueOptions = [];
      $entity_type_id = $this->definition['options entity type'] ?? NULL;
      if ($entity_type_id) {
        $entities = \Drupal::entityTypeManager()
          ->getStorage($entity_type_id)
          ->loadMultiple();
        foreach ($entities as $id => $entity) {
          $this->valueOptions[$id] = $entity->label();
        }
      }
    }
    return $this->valueOptions;
  }

}
MyModuleViewsHooks.php
<?php

namespace Drupal\my_module\Hook;

use Drupal\Core\Hook\Attribute\Hook;

/**
 * Views hook implementations for my_module.
 */
class MyModuleViewsHooks {

  /**
   * Implements hook_views_data_alter().
   */
  #[Hook('views_data_alter')]
  public function viewsDataAlter(array &$data): void {
    // Node field storing user role IDs.
    $data['node__field_roles']['field_roles_value']['filter']['id'] = 'my_module_entity_options';
    // Entity type whose entities are offered as filter options.
    // Change it to reuse the filter, e.g. 'profile_type' or 'group_type'.
    $data['node__field_roles']['field_roles_value']['filter']['options entity type'] = 'user_role';
  }

}
static_or_nonstatic_that_is_the_question.php
// Static way.      
$slot_paragraph = Paragraph::create([
    'type' => 'slot',
]);
$slot_paragraph->save();

// Nonstatic way.      
$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage('paragraphs');
$slot_paragraph = $storage->create([
    'type' => 'slot',
]);
$slot_paragraph->save();
entity_key_id.php
$entity_type_id = 'node';
$definition = \Drupal::entityTypeManager()->getDefinition($entity_type_id);
$id_key = $definition->getKey('id');
10.09.2026 | Mathias Grab

Get "key" from entity type

my_module.install
<?php

/**
 * Change my_basefield max_length to 255.
 */
function my_module_update_10001() {
  $entity_type_id = 'my_entity';
  $field_name = 'my_basefield';
  $field_length = 255;

  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
  $schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  /** @var \Drupal\Core\Entity\EntityFieldManager $entity_field_manager */
  $entity_field_manager = \Drupal::service('entity_field.manager');
  /** @var Drupal\Core\Field\BaseFieldDefinition[] $base_field_definitions */
  $base_field_definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type_id);
  $schema_repository->setLastInstalledFieldStorageDefinition($base_field_definitions[$field_name]);
  $field_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);

  // Update the serialized schema property.
  $rc = new \ReflectionClass($field_storage_definitions[$field_name]);
  $schema_property = $rc->getProperty('schema');
  $schema_property->setAccessible(TRUE);
  $schema = $field_storage_definitions[$field_name]->getSchema();
  $schema['columns']['value']['length'] = $field_length;
  $schema_property->setValue($field_storage_definitions[$field_name], $schema);

  // Update the field definition in the last installed schema repository.
  $schema_repository->setLastInstalledFieldStorageDefinitions($entity_type_id, $field_storage_definitions);

  // Update the storage schema.
  $key_value = \Drupal::keyValue('entity.storage_schema.sql');
  $key_name = $entity_type_id . '.field_schema_data.' . $field_name;
  $storage_schema = $key_value->get($key_name);
  // Update all tables where the field is present.
  foreach ($storage_schema as &$table_schema) {
    $table_schema['fields'][$field_name]['length'] = $field_length;
  }
  $key_value->set($key_name, $storage_schema);

  // Update the database tables where the field is part of.
  $db = Drupal::database();
  foreach ($storage_schema as $table_name => $table_schema) {
    $db->schema()->changeField($table_name, $field_name, $field_name, $table_schema['fields'][$field_name]);
  }

}
getBundlesWithViewmode.php
  private function getBundlesWithViewmode(string $entity_type, string $view_mode): array {
    $bundles = [];

    foreach (\Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type) as $bundle => $info) {
      $view_modes = \Drupal::service('entity_display.repository')->getViewModeOptionsByBundle($entity_type, $bundle);
      if (isset($view_modes[$view_mode])) {
        $bundles[] = $bundle;
      }
    }

    return $bundles;
  }
  
entity_field_value.php
// For single value fields.
$entity->field_name->value;

// If you don`t know the main property.
$main_property_name = $entity->field_name->getMainPropertyName();

// For multiple value fields.
$entity->field_name->getValue();
12.02.2026 | Michael Ebert

Get value of an entity field

⚠️ WARNING: Potential data loss. Use with caution.

scp_base.module
/**
 * Run missing database schema updates.
 */
function mymodule_update_10001(&$sandbox) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type_manager->clearCachedDefinitions();
  $change_summary = \Drupal::service('entity.definition_update_manager')->getChangeSummary();
  foreach ($change_summary as $entity_type_id => $change_list) {
    $entity_type = $entity_type_manager->getDefinition($entity_type_id);
    \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type);
  }
}
07.05.2026 | Peter Gerken

Run missing entity Schema updates

my_module.module
// Assume $entity is your translatable entity object.
$default_language_entity = $entity->getUntranslated();
$default_language_code = $default_language_entity->language()->getId();

08.10.2026 | Pascal Crott

Get default langcode from entity