ModulesListForm::buildRow

protected ModulesListForm::buildRow(array $modules, Extension $module, $distribution)

Builds a table row for the system modules page.

Parameters

array $modules: The list existing modules.

\Drupal\Core\Extension\Extension $module: The module for which to build the form row.

$distribution:

Return value

array The form row for the given module.

File

core/modules/system/src/Form/ModulesListForm.php, line 201

Class

ModulesListForm
Provides module installation interface.

Namespace

Drupal\system\Form

Code

protected function buildRow(array $modules, Extension $module, $distribution) {
  // Set the basic properties.
  $row['#required'] = array();
  $row['#requires'] = array();
  $row['#required_by'] = array();

  $row['name']['#markup'] = $module->info['name'];
  $row['description']['#markup'] = $this->t($module->info['description']);
  $row['version']['#markup'] = $module->info['version'];

  // Generate link for module's help page. Assume that if a hook_help()
  // implementation exists then the module provides an overview page, rather
  // than checking to see if the page exists, which is costly.
  if ($this->moduleHandler->moduleExists('help') && $module->status && in_array($module->getName(), $this->moduleHandler->getImplementations('help'))) {
    $row['links']['help'] = array(
      '#type' => 'link',
      '#title' => $this->t('Help'),
      '#url' => Url::fromRoute('help.page', ['name' => $module->getName()]),
      '#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => $this->t('Help'))),
    );
  }

  // Generate link for module's permission, if the user has access to it.
  if ($module->status && $this->currentUser->hasPermission('administer permissions') && $this->permissionHandler->moduleProvidesPermissions($module->getName())) {
    $row['links']['permissions'] = array(
      '#type' => 'link',
      '#title' => $this->t('Permissions'),
      '#url' => Url::fromRoute('user.admin_permissions'),
      '#options' => array('fragment' => 'module-' . $module->getName(), 'attributes' => array('class' => array('module-link', 'module-link-permissions'), 'title' => $this->t('Configure permissions'))),
    );
  }

  // Generate link for module's configuration page, if it has one.
  if ($module->status && isset($module->info['configure'])) {
    $route_parameters = isset($module->info['configure_parameters']) ? $module->info['configure_parameters'] : array();
    if ($this->accessManager->checkNamedRoute($module->info['configure'], $route_parameters, $this->currentUser)) {
      $row['links']['configure'] = array(
        '#type' => 'link',
        '#title' => $this->t('Configure <span class="visually-hidden">the @module module</span>', ['@module' => $module->info['name']]),
        '#url' => Url::fromRoute($module->info['configure'], $route_parameters),
        '#options' => array(
          'attributes' => array(
            'class' => array('module-link', 'module-link-configure'),
          ),
        ),
      );
    }
  }

  // Present a checkbox for installing and indicating the status of a module.
  $row['enable'] = array(
    '#type' => 'checkbox',
    '#title' => $this->t('Install'),
    '#default_value' => (bool) $module->status,
    '#disabled' => (bool) $module->status,
  );

  // Disable the checkbox for required modules.
  if (!empty($module->info['required'])) {
    // Used when displaying modules that are required by the installation profile
    $row['enable']['#disabled'] = TRUE;
    $row['#required_by'][] = $distribution . (!empty($module->info['explanation']) ? ' (' . $module->info['explanation'] . ')' : '');
  }

  // Check the compatibilities.
  $compatible = TRUE;

  // Initialize an empty array of reasons why the module is incompatible. Add
  // each reason as a separate element of the array.
  $reasons = array();

  // Check the core compatibility.
  if ($module->info['core'] != \Drupal::CORE_COMPATIBILITY) {
    $compatible = FALSE;
    $reasons[] = $this->t('This version is not compatible with Drupal @core_version and should be replaced.', array(
      '@core_version' => \Drupal::CORE_COMPATIBILITY,
    ));
  }

  // Ensure this module is compatible with the currently installed version of PHP.
  if (version_compare(phpversion(), $module->info['php']) < 0) {
    $compatible = FALSE;
    $required = $module->info['php'] . (substr_count($module->info['php'], '.') < 2 ? '.*' : '');
    $reasons[] = $this->t('This module requires PHP version @php_required and is incompatible with PHP version @php_version.', array(
      '@php_required' => $required,
      '@php_version' => phpversion(),
    ));
  }

  // If this module is not compatible, disable the checkbox.
  if (!$compatible) {
    $status = implode(' ', $reasons);
    $row['enable']['#disabled'] = TRUE;
    $row['description']['#markup'] = $status;
    $row['#attributes']['class'][] = 'incompatible';
  }

  // If this module requires other modules, add them to the array.
  foreach ($module->requires as $dependency => $version) {
    if (!isset($modules[$dependency])) {
      $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">missing</span>)', array('@module' => Unicode::ucfirst($dependency)));
      $row['enable']['#disabled'] = TRUE;
    }
    // Only display visible modules.
    elseif (empty($modules[$dependency]->hidden)) {
      $name = $modules[$dependency]->info['name'];
      // Disable the module's checkbox if it is incompatible with the
      // dependency's version.
      if ($incompatible_version = drupal_check_incompatibility($version, str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) {
        $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> version @version)', array(
          '@module' => $name . $incompatible_version,
          '@version' => $modules[$dependency]->info['version'],
        ));
        $row['enable']['#disabled'] = TRUE;
      }
      // Disable the checkbox if the dependency is incompatible with this
      // version of Drupal core.
      elseif ($modules[$dependency]->info['core'] != \Drupal::CORE_COMPATIBILITY) {
        $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', array(
          '@module' => $name,
        ));
        $row['enable']['#disabled'] = TRUE;
      }
      elseif ($modules[$dependency]->status) {
        $row['#requires'][$dependency] = $this->t('@module', array('@module' => $name));
      }
      else {
        $row['#requires'][$dependency] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $name));
      }
    }
  }

  // If this module is required by other modules, list those, and then make it
  // impossible to disable this one.
  foreach ($module->required_by as $dependent => $version) {
    if (isset($modules[$dependent]) && empty($modules[$dependent]->info['hidden'])) {
      if ($modules[$dependent]->status == 1 && $module->status == 1) {
        $row['#required_by'][$dependent] = $this->t('@module', array('@module' => $modules[$dependent]->info['name']));
        $row['enable']['#disabled'] = TRUE;
      }
      else {
        $row['#required_by'][$dependent] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $modules[$dependent]->info['name']));
      }
    }
  }

  return $row;
}
doc_Drupal
2016-10-29 09:29:37
Comments
Leave a Comment

Please login to continue.