You are here

tca.module in Token Content Access 8

Same filename and directory in other branches
  1. 7 tca.module
  2. 2.0.x tca.module

Contains tca.module.

File

tca.module
View source
<?php

/**
 * @file
 * Contains tca.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Implements hook_help().
 */
function tca_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the tca module.
    case 'help.page.tca':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Basic functionality that provides access control for entities based on a URL token.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_form_alter().
 */
function tca_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $tca_plugin = \Drupal::service('plugin.manager.tca_plugin');
  $affected_types = $tca_plugin
    ->loadSupportedEntityTypes();
  $affected_bundle_types = $tca_plugin
    ->loadSupportedBundleEntityTypes();
  if ($form_state
    ->getFormObject() instanceof EntityFormInterface) {
    $current_type = $form_state
      ->getFormObject()
      ->getEntity()
      ->getEntityTypeId();
    $current_operation = $form_state
      ->getFormObject()
      ->getOperation();
    $administer_permitted = \Drupal::currentUser()
      ->hasPermission('tca administer ' . $current_type);

    // Skip delete and cencel forms.
    if (!in_array($current_operation, [
      'delete',
      'cancel',
    ])) {
      if (in_array($current_type, $affected_types) && $administer_permitted || in_array($current_type, $affected_bundle_types) || $current_operation === 'add' && _tca_is_forced($form_state
        ->getFormObject()
        ->getEntity())) {
        \Drupal::service('tca.form_mangler')
          ->addTcaSettingsToEntityForm($form, $form_state
          ->getFormObject()
          ->getEntity(), $form_state, $form_id);
        $form['#attached']['library'][] = 'tca/form';
      }
    }
  }
}

/**
 * Implements hook_entity_access().
 */
function tca_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  $neutral = AccessResult::neutral()
    ->addCacheableDependency($entity)
    ->addCacheContexts([
    'url.path',
  ]);
  if ($operation !== 'view') {
    return $neutral;
  }
  $user_token = \Drupal::request()->query
    ->get('tca');
  return \Drupal::service('tca.tca_access_check')
    ->access($entity, $user_token, $account);
}

/**
 * Define etrafields that will be used for storeing settings.
 */
function _tca_extra_field_definitions() {
  $fields['tca_active'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('TCA Active'))
    ->setDefaultValue(0)
    ->setStorageRequired(TRUE);
  $fields['tca_public'] = BaseFieldDefinition::create('boolean')
    ->setLabel(t('TCA Public'))
    ->setDefaultValue(0)
    ->setDescription(t('Allows content to be viewed without the "View published content" permission'))
    ->setStorageRequired(TRUE);
  $fields['tca_token'] = BaseFieldDefinition::create('string')
    ->setLabel(t('TCA Token'))
    ->setDefaultValue('')
    ->setStorageRequired(TRUE);
  return $fields;
}

/**
 * Returns tca_settings force option.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity object.
 *
 * @return bool
 *   TRUE if usage of token is enforced, FALSE otherwise.
 */
function _tca_is_forced(EntityInterface $entity) {
  if ($entity
    ->getEntityType()
    ->getBundleEntityType() === NULL) {
    return FALSE;
  }
  $tcaSettingsManager = \Drupal::service('tca.tca_settings_manager');
  $settings = $tcaSettingsManager
    ->loadSettingsAsConfig($entity
    ->getEntityType()
    ->getBundleEntityType(), $entity
    ->bundle());
  return (bool) $settings
    ->get('force');
}

/**
 * Implements hook_entity_base_field_info().
 */
function tca_entity_base_field_info(EntityTypeInterface $entity_type) {
  $plugin = \Drupal::service('plugin.manager.tca_plugin')
    ->createInstanceByEntityType($entity_type
    ->id());
  if ($plugin && $plugin
    ->isFieldable()) {
    $fields = _tca_extra_field_definitions();
    return $fields;
  }
}

/**
 * Implements hook_modules_installed().
 */
function tca_modules_installed($modules) {
  $plugin_manager = \Drupal::service('plugin.manager.tca_plugin');
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  foreach ($modules as $module) {
    $plugin_definitions = $plugin_manager
      ->loadDefinitionsByModuleName($module);
    foreach ($plugin_definitions as $plugin_definition) {
      $provider = $plugin_definition['provider'];
      $entity_type_id = $plugin_definition['entityType'];
      $plugin = $plugin_manager
        ->createInstanceByEntityType($entity_type_id);
      if ($plugin && $plugin
        ->isFieldable()) {
        $fields = _tca_extra_field_definitions();
        foreach ($fields as $name => $storage_definition) {
          $definition_update_manager
            ->installFieldStorageDefinition($name, $entity_type_id, $provider, $storage_definition);
        }
      }
    }
  }
}

/**
 * Implements hook_module_preuninstall().
 */
function tca_module_preuninstall($module) {
  $plugin_manager = \Drupal::service('plugin.manager.tca_plugin');
  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
  $plugin_definitions = $plugin_manager
    ->loadDefinitionsByModuleName($module);
  foreach ($plugin_definitions as $plugin_definition) {
    $entity_type_id = $plugin_definition['entityType'];
    $plugin = $plugin_manager
      ->createInstanceByEntityType($entity_type_id);
    if ($plugin && $plugin
      ->isFieldable()) {
      $fields = _tca_extra_field_definitions();
      foreach ($fields as $name => $storage_definition) {
        $storage_definition = $definition_update_manager
          ->getFieldStorageDefinition($name, $entity_type_id);
        $definition_update_manager
          ->uninstallFieldStorageDefinition($storage_definition);
      }
    }
  }
}

Functions

Namesort descending Description
tca_entity_access Implements hook_entity_access().
tca_entity_base_field_info Implements hook_entity_base_field_info().
tca_form_alter Implements hook_form_alter().
tca_help Implements hook_help().
tca_modules_installed Implements hook_modules_installed().
tca_module_preuninstall Implements hook_module_preuninstall().
_tca_extra_field_definitions Define etrafields that will be used for storeing settings.
_tca_is_forced Returns tca_settings force option.