You are here

function template_preprocess_system_modules_details in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/system.admin.inc \template_preprocess_system_modules_details()

Prepares variables for the module details templates.

Default template: system-modules-details.html.twig.

Parameters

$variables: An associative array containing:

  • form: A render element representing the form. The main form element represents a package, and child elements of the form are individual projects. Each project (or module) is an associative array containing the following elements:

    • name: The name of the module.
    • enable: A checkbox for enabling the module.
    • description: A description of the module.
    • version: The version of the module.
    • links: Administration links provided by the module.
    • #requires: A list of modules that the project requires.
    • #required_by: A list of modules and themes that require the project.
    • #attributes: A list of attributes for the module wrapper.

See also

\Drupal\system\Form\ModulesListForm

File

core/modules/system/system.admin.inc, line 132
Admin page callbacks for the system module.

Code

function template_preprocess_system_modules_details(&$variables) {
  $form = $variables['form'];

  // Identify modules that are depended on by themes.
  // Added here instead of ModuleHandler to avoid recursion.
  $themes = \Drupal::service('extension.list.theme')
    ->getList();
  foreach ($themes as $theme) {
    foreach ($theme->info['dependencies'] as $dependency) {
      if (isset($form[$dependency])) {

        // Add themes to the module's required by list.
        $form[$dependency]['#required_by'][] = $theme->status ? t('@theme', [
          '@theme (theme)' => $theme->info['name'],
        ]) : t('@theme (theme) (<span class="admin-disabled">disabled</span>)', [
          '@theme' => $theme->info['name'],
        ]);
      }
    }
  }
  $variables['modules'] = [];

  // Iterate through all the modules, which are children of this element.
  foreach (Element::children($form) as $key) {

    // Stick the key into $module for easier access.
    $module = $form[$key];
    unset($module['enable']['#title']);
    $module['#requires'] = array_filter($module['#requires']);
    $module['#required_by'] = array_filter($module['#required_by']);

    // Add the checkbox to allow installing new modules and to show the
    // installation status of the module.
    $module['checkbox'] = $module['enable'];

    // Add the module label and expand/collapse functionality.
    $id = Html::getUniqueId('module-' . $key);
    $module['id'] = $id;
    $module['enable_id'] = $module['enable']['#id'];

    // @todo Remove early rendering and use safe_join in the Twig template once
    //   https://www.drupal.org/node/2579091 is fixed.
    $renderer = \Drupal::service('renderer');
    $machine_name_render = [
      '#prefix' => '<span dir="ltr" class="table-filter-text-source">',
      '#plain_text' => $key,
      '#suffix' => '</span>',
    ];
    $module['machine_name'] = $renderer
      ->render($machine_name_render);
    if (!empty($module['#requires'])) {
      $requires = [
        '#theme' => 'item_list',
        '#items' => $module['#requires'],
        '#context' => [
          'list_style' => 'comma-list',
        ],
      ];
      $module['requires'] = $renderer
        ->render($requires);
    }
    if (!empty($module['#required_by'])) {
      $required_by = [
        '#theme' => 'item_list',
        '#items' => $module['#required_by'],
        '#context' => [
          'list_style' => 'comma-list',
        ],
      ];
      $module['required_by'] = $renderer
        ->render($required_by);
    }
    if (!empty($module['version'])) {
      $module['version'] = $renderer
        ->render($module['version']);
    }
    $module['attributes'] = new Attribute($module['#attributes']);
    $variables['modules'][] = $module;
  }
}