Skip to content
Snippets Groups Projects
Forked from project / cloud
3111 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
cloud.module 2.39 KiB
<?php

/**
 * @file
 * Enables users to access the Privately managed clouds.
 *
 * Provides common functionality for cloud management.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\ViewExecutable;

/**
 * Implements hook_help().
 */
function cloud_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.cloud':
      $output = '<p>' . t('The cloud module creates a user interface for users to manage clouds. Users can Create Instances,  Describe Instances etc.') . '</p>';
      return $output;

    default:
      return '';
  }
}

/**
 * Implements hook_theme().
 */
function cloud_theme() {
  $theme = [];
  $theme['cloud_config'] = [
    'render element' => 'elements',
    'file' => 'cloud_config.page.inc',
    'template' => 'cloud_config',
  ];
  $theme['cloud_config_content_add_list'] = [
    'render element' => 'content',
    'variables' => ['content' => NULL],
    'file' => 'cloud_config.page.inc',
  ];
  return $theme;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function cloud_theme_suggestions_cloud_config(array $variables) {
  $suggestions = [];
  $entity = $variables['elements']['#cloud_config'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');

  $suggestions[] = 'cloud_config__' . $sanitized_view_mode;
  $suggestions[] = 'cloud_config__' . $entity->bundle();
  $suggestions[] = 'cloud_config__' . $entity->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'cloud_config__' . $entity->id();
  $suggestions[] = 'cloud_config__' . $entity->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Implements hook_views_pre_render().
 *
 * This is a workaround to implement row level access control,
 * until this issue is resolved:
 * https://www.drupal.org/project/entity/issues/2909970
 * Loop through the results,
 * and call hasPermissions() with the entity's cloud_context.
 * Unset the entity if the user does not have permissions.
 */
function cloud_views_pre_render(ViewExecutable $view) {
  $account = \Drupal::currentUser();
  if ($view->id() == 'cloud_listing' || $view->id() == 'server_template_listing') {
    foreach ($view->result as $key => $result) {
      /* @var \Drupal\cloud\Entity\CloudConfigInterface $cloud */
      $cloud = $result->_entity;
      if (!$account->hasPermission('view ' . $cloud->getCloudContext())) {
        unset($view->result[$key]);
      }
    }
  }
}