You are here

public function WebformLibrariesManager::requirements in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/WebformLibrariesManager.php \Drupal\webform\WebformLibrariesManager::requirements()

Get third party libraries status for hook_requirements and drush.

Return value

array An associative array of third party libraries keyed by library name.

Overrides WebformLibrariesManagerInterface::requirements

File

src/WebformLibrariesManager.php, line 85

Class

WebformLibrariesManager
Webform libraries manager.

Namespace

Drupal\webform

Code

public function requirements($cli = FALSE) {
  $cdn = $this->configFactory
    ->get('webform.settings')
    ->get('requirements.cdn') ?: FALSE;
  $libraries = $this
    ->getLibraries();

  // Defined REQUIREMENT constants which may not be loaded.
  // @see /private/var/www/sites/d8_webform/web/core/includes/install.inc
  if (!defined('REQUIREMENT_OK')) {
    define('REQUIREMENT_INFO', -1);
    define('REQUIREMENT_OK', 0);
    define('REQUIREMENT_WARNING', 1);
    define('REQUIREMENT_ERROR', 2);
  }

  // Track stats.
  $severity = REQUIREMENT_OK;
  $stats = [
    '@total' => count($libraries),
    '@installed' => 0,
    '@excluded' => 0,
    '@missing' => 0,
  ];

  // Build library info array.
  $info = [
    '#prefix' => '<p><hr/></p><dl>',
    '#suffix' => '</dl>',
  ];
  foreach ($libraries as $library_name => $library) {

    // Excluded.
    if ($this
      ->isExcluded($library_name)) {
      $stats['@excluded']++;
      continue;
    }
    $library_exists = $this
      ->exists($library_name);
    $library_path = $library_exists ? '/' . $this
      ->find($library_name) : '/libraries/' . $library_name;
    $t_args = [
      '@title' => $library['title'],
      '@version' => $library['version'],
      '@path' => $library_path,
      ':download_href' => $library['download_url']
        ->toString(),
      ':homepage_href' => $library['homepage_url']
        ->toString(),
      ':external_href' => 'https://www.drupal.org/docs/8/theming-drupal-8/adding-stylesheets-css-and-javascript-js-to-a-drupal-8-theme#external',
      ':install_href' => $this->moduleHandler
        ->moduleExists('help') ? Url::fromRoute('help.page', [
        'name' => 'webform',
      ], [
        'fragment' => 'libraries',
      ])
        ->toString() : 'https://www.drupal.org/docs/8/modules/webform/webform-libraries',
      ':settings_libraries_href' => Url::fromRoute('webform.config.libraries')
        ->toString(),
      ':settings_elements_href' => Url::fromRoute('webform.config.elements')
        ->toString(),
    ];
    if (!empty($library['module'])) {

      // Installed by module.
      $t_args['@module'] = $library['module'];
      $t_args[':module_href'] = 'https://www.drupal.org/project/' . $library['module'];
      $stats['@installed']++;
      $title = $this
        ->t('<strong>@title</strong> (Installed)', $t_args);
      $description = $this
        ->t('The <a href=":homepage_href">@title</a> library is installed by the <b><a href=":module_href">@module</a></b> module.', $t_args);
    }
    elseif ($library_exists) {

      // Installed.
      $stats['@installed']++;
      $title = $this
        ->t('<strong>@title @version</strong> (Installed)', $t_args);
      $description = $this
        ->t('The <a href=":homepage_href">@title</a> library is installed in <b>@path</b>.', $t_args);
    }
    elseif ($cdn) {

      // Missing.
      $stats['@missing']++;
      $title = $this
        ->t('<span class="color-warning"><strong>@title @version</strong> (CDN).</span>', $t_args);
      $description = $this
        ->t('Please download the <a href=":homepage_href">@title</a> library from <a href=":download_href">:download_href</a> and copy it to <b>@path</b> or use <a href=":install_href">Drush</a> to install this library.', $t_args);
      $severity = REQUIREMENT_WARNING;
    }
    else {

      // CDN.
      $stats['@missing']++;
      $title = $this
        ->t('<strong>@title @version</strong> (CDN).', $t_args);
      $description = $this
        ->t('The <a href=":homepage_href">@title</a> library is <a href=":external_href">externally hosted libraries</a> and loaded via a Content Delivery Network (CDN).', $t_args);
    }
    $info[$library_name] = [];
    $info[$library_name]['title'] = [
      '#markup' => $title,
      '#prefix' => '<dt>',
      '#suffix' => '</dt>',
    ];
    $info[$library_name]['description'] = [
      '#prefix' => '<dd>',
      '#suffix' => '</dd>',
    ];
    $info[$library_name]['description']['content'] = [
      '#markup' => $description,
    ];
    if (!empty($library['notes'])) {
      $info[$library_name]['description']['notes'] = [
        '#markup' => $library['notes'],
        '#prefix' => '<div><em>(',
        '#suffix' => '}</em></div>',
      ];
    }
    if (!empty($library['deprecated'])) {
      $info[$library_name]['description']['status'] = [
        '#markup' => $library['deprecated'],
        '#prefix' => '<div class="color-warning"><strong>',
        '#suffix' => '</strong></div>',
      ];
    }
  }

  // Description.
  $description = [
    'info' => $info,
  ];
  if (!$cli && $severity === REQUIREMENT_WARNING) {
    $description['cdn'] = [
      '#markup' => $this
        ->t('<a href=":href">Disable CDN warning</a>', [
        ':href' => Url::fromRoute('webform.config.advanced')
          ->toString(),
      ]),
    ];
  }
  return [
    'webform_libraries' => [
      'title' => $this
        ->t('Webform: External libraries'),
      'value' => $this
        ->t('@total libraries (@installed installed; @excluded excluded; @missing CDN)', $stats),
      'description' => $this->renderer
        ->renderPlain($description),
      'severity' => $severity,
    ],
  ];
}