php

sort_by_weight.php
// ...

$build['content_1'] = [
    '#markup' => "content 1",
    '#custom_weight_property' => 10,
];

$build['content_2'] = [
    '#markup' => "content 2",
    '#custom_weight_property' => 5,
];

usort($build, function ($a, $b) {
    return SortArray::sortByKeyInt($a, $b, '#custom_weight_property');
});

The output looks like this:

content 2
content 1

Just a hint:

The custom weight system controls the display order of elements, where lower weight values appear earlier, as shown in the example: content_2 (with weight 5) is rendered before content_1 (with weight 10).
 

23.04.2026 | Lothar Ferreira Neumann

Sort render array by custom weight property

stringarray.php
<?php

declare(strict_types=1);

$string = "Example";

// This will print out "E".
echo($string[0]);

// You can also access the letters in a loop.
for ($i = 0; $i < strlen($string); $i++) {
    echo($string[$i]);
}
17.01.2025 | Lothar Ferreira Neumann

How to get individual characters from a string

my_module.module
function my_module_local_tasks_alter(&$local_tasks) {
  if (isset($local_tasks['entity.section_library_template.collection'])) {
    $local_tasks['entity.section_library_template.collection']['weight'] = 110;
  }
    
  if (isset($local_tasks['feeds.admin'])) {
    $local_tasks['feeds.admin']['weight'] = 120;
  }

}

11.06.2026 | Michael Ebert

Change the order of local tasks

my_theme.theme
/**
 * Implements hook_preprocess_HOOK() for layout.html.twig.
 */
function hook_preprocess_layout(&$variables) {
  $layout = $variables['layout'];
  foreach ($layout->getRegionNames() as $region_name) {
    if (array_key_exists($region_name, $variables['content'])) {
      uasort($variables['content'][$region_name], [\Drupal\Component\Utility\SortArray::class, 'sortByWeightProperty']);
    }
  }
}
get_all_contrib_modules.php
<?php

/**
 * Get a list of all contrib modules.
 * 
 @param string $context
 *   The context of the module like contrib, custom or core.
 *   Only the parameter 'contrib', 'custom', or 'core' are 
 *   available parameters.
 * @return array
 *   Returns an array with all contrib modules' machine names.
 */
public function getAvailableModules(string $context): array {
  $context = 'contrib';
  $module_list = \Drupal::service('module_handler')->getModuleList();
  $modules = [];

  foreach ($module_list as $module) {
    if (str_contains($module->getPath(), 'contrib')) {
      $modules[$module->getName()] = $module;
    }
  }

  return $modules;
}
error_handling.php
<?php

$error_message = t('There is an error with this.');

\Drupal::messenger()->addError($error_message));
\Drupal::logger('my_module')->log('error', $error_message);

test-db.php
<?php

include('web/sites/default/settings.php');

$dbHostName = $databases['default']['default']['host'];
$dataBaseName = $databases['default']['default']['database'];
$dbUserName = $databases['default']['default']['username'];
$dbPassword = $databases['default']['default']['password'];
$dbPort = $databases['default']['default']['port'];

$pdo = new PDO('mysql:host='.$dbHostName.';port='.$dbPort.';dbname='.$dataBaseName.'', $dbUserName, $dbPassword, array(
  // PDO::MYSQL_ATTR_SSL_CA    =>'ca.cert',
  PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => TRUE
  )
);

$query = $pdo->query('SHOW TABLES;');
print_r($query->fetchAll());

return $pdo;

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;
  }