Do you want to know how to Drupal?

Let's Drupal

How to remove (uninstall) entity type in Drupal 8?

If we don't need some entity type anymore we can easily remove it.
// Get update manager.
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
// Get entity type.
$entity_type = $definition_update_manager->getEntityType('your_entity_type_id');
if ($entity_type ) {
    // Uninstall entity type.


How to update entity field in Drupal 8?

Sometimes we need to update some settings for a field. In our case we will be update drop down field
function your_module_update_8001() {
  // First we need to get update manager.
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  // Load the storage definition of the field.
  $field_storage = $definition_update_manager->getFieldStorageDefinition('field_name',
  // Set a new list of options for the list field.
    'allowed_values' => [
      'link' => 'Link',
      'posts' => 'Posts',
      'events' => 'Events',
      'page' => 'Page',

  // Update the field storage definition.


How to create programmatically a redirect for Redirect module in Drupal 8?

If use Redirect module sometimes you need to create URL programmatically. You can do it using the code bellow

use Drupal\redirect\Entity\Redirect;

    'redirect_source' => 'your_custom_url', // Set your custom URL.
    'redirect_redirect' => 'internal:/node/[NID]', // Set internal path to a node for example.
    'language' => 'und', // Set the current language or undefined.
    'status_code' => '301', // Set HTTP code.


How to load entity by a specific field value in Drupal 9?

Quite often it is required to get data by a field value. In our example I will use nodes, but it can be any entity type.

In first example we load entities by based fields. For example vid, type, language and etc.

I want to get all the nodes of the type post  for my custom block. For this we will use EntityTypeManager class

// Load a storage for node entity type
$items = \Drupal::entityTypeManager() 
      ->loadByProperties(['type' => 'post']); // load all nodes with the node type

After we can run through the items and apply our custom logic here.

In the second example, we will get entities based on custom fields. For example we have field "field_type"

$query = $this->entityTypeManager->getStorage('node')->getQuery();
$query->condition('status', 1)
    ->condition('changed', REQUEST_TIME, '<')
    ->condition('field_type', 'post');

$nids = $query->execute();

More examples can be found here: EntityFieldQuery