You are here

cloudflare.install in CloudFlare 8

Same filename and directory in other branches
  1. 7.2 cloudflare.install

Install, update, and uninstall functions for the CloudFlare module.

File

cloudflare.install
View source
<?php

/**
 * @file
 * Install, update, and uninstall functions for the CloudFlare module.
 */
use Drupal\Core\Url;

/**
 * Implements hook_requirements().
 */
function cloudflare_requirements($phase) {
  $requirements = [];
  if (!class_exists('\\CloudFlarePhpSdk\\ApiEndpoints\\ZoneApi')) {
    $requirements['cloudflare_library'] = [
      'title' => t('CloudFlare - Php Sdk'),
      'description' => t('CloudFlare requires the d8-contrib-modules/cloudflarephpsdk library.'),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  if ($phase == 'runtime') {
    $config = \Drupal::config('cloudflare.settings');
    $valid_credentials = $config
      ->get('valid_credentials') === TRUE;
    if (!$valid_credentials) {
      $link_to_settings = Url::fromRoute('cloudflare.admin_settings_form')
        ->toString();
      $message = t('Missing valid credentials for CloudFlare. Please go <a href="@link_to_settings">here</a> to set.', [
        '@link_to_settings' => $link_to_settings,
      ]);
      $requirements['cloudflare'] = [
        'title' => t('CloudFlare - Credentials'),
        'severity' => REQUIREMENT_ERROR,
        'description' => $message,
      ];
    }
    if (\Drupal::moduleHandler()
      ->moduleExists('cloudflare_zone_ui')) {
      \Drupal::service('module_installer')
        ->uninstall([
        'cloudflare_zone_ui',
      ]);
    }
  }
  return $requirements;
}

/**
 * Installs ctools module.  Uninstalls cloudflare_zone_ui.
 */
function cloudflare_update_8001(&$sandbox) {
  \Drupal::service('module_installer')
    ->install([
    'ctools',
  ]);
}

/**
 * Grants the 'administer cloudflare' permission to eligible roles.
 */
function cloudflare_update_8002(&$sandbox) {
  try {

    /* @var $user_role_storage \Drupal\user\RoleStorageInterface */
    $user_role_storage = \Drupal::entityTypeManager()
      ->getStorage('user_role');

    /* @var $roles array|\Drupal\user\RoleInterface[] */
    $roles = $user_role_storage
      ->loadMultiple();
    foreach ($roles as $role) {
      if ($role
        ->hasPermission('access administration pages')) {
        $role
          ->grantPermission('administer cloudflare')
          ->save();
      }
    }
  } catch (EntityStorageException $e) {
    throw new UpdateException('Update 8002 failed with the following: ' . $e
      ->getMessage(), $e
      ->getCode(), $e);
  } catch (InvalidPluginDefinitionException $e) {
    throw new UpdateException('Update 8002 failed with the following: ' . $e
      ->getMessage(), $e
      ->getCode(), $e);
  } catch (PluginNotFoundException $e) {
    throw new UpdateException('Update 8002 failed with the following: ' . $e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}

Functions

Namesort descending Description
cloudflare_requirements Implements hook_requirements().
cloudflare_update_8001 Installs ctools module. Uninstalls cloudflare_zone_ui.
cloudflare_update_8002 Grants the 'administer cloudflare' permission to eligible roles.