wotapi-8.x-1.x-dev/wotapi.module

wotapi.module
<?php

/**
 * @file
 * Module implementation file.
 */

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\FieldConfigInterface;
use Drupal\wotapi\Routing\Routes;

/**
 * Array key for denoting type-based filtering access.
 *
 * Array key for denoting access to filter among all entities of a given type,
 * regardless of whether they are published or enabled, and regardless of
 * their owner.
 *
 * @see hook_wotapi_entity_filter_access()
 * @see hook_wotapi_ENTITY_TYPE_filter_access()
 */
const WOTAPI_FILTER_AMONG_ALL = 'filter_among_all';

/**
 * Array key for denoting type-based published-only filtering access.
 *
 * Array key for denoting access to filter among all published entities of a
 * given type, regardless of their owner.
 *
 * This is used when an entity type has a "published" entity key and there's a
 * query condition for the value of that equaling 1.
 *
 * @see hook_wotapi_entity_filter_access()
 * @see hook_wotapi_ENTITY_TYPE_filter_access()
 */
const WOTAPI_FILTER_AMONG_PUBLISHED = 'filter_among_published';

/**
 * Array key for denoting type-based enabled-only filtering access.
 *
 * Array key for denoting access to filter among all enabled entities of a
 * given type, regardless of their owner.
 *
 * This is used when an entity type has a "status" entity key and there's a
 * query condition for the value of that equaling 1.
 *
 * For the User entity type, which does not have a "status" entity key, the
 * "status" field is used.
 *
 * @see hook_wotapi_entity_filter_access()
 * @see hook_wotapi_ENTITY_TYPE_filter_access()
 */
const WOTAPI_FILTER_AMONG_ENABLED = 'filter_among_enabled';

/**
 * Array key for denoting type-based owned-only filtering access.
 *
 * Array key for denoting access to filter among all entities of a given type,
 * regardless of whether they are published or enabled, so long as they are
 * owned by the user for whom access is being checked.
 *
 * When filtering among User entities, this is used when access is being
 * checked for an authenticated user and there's a query condition
 * limiting the result set to just that user's entity object.
 *
 * When filtering among entities of another type, this is used when all of the
 * following conditions are met:
 * - Access is being checked for an authenticated user.
 * - The entity type has an "owner" entity key.
 * - There's a filter/query condition for the value equal to the user's ID.
 *
 * @see hook_wotapi_entity_filter_access()
 * @see hook_wotapi_ENTITY_TYPE_filter_access()
 */
const WOTAPI_FILTER_AMONG_OWN = 'filter_among_own';

/**
 * Implements hook_help().
 */
function wotapi_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.wotapi':
      $output = '<h3>' . t('About') . '</h3>';

      return $output;
  }
  return NULL;
}

/**
 * Implements hook_modules_installed().
 */
function wotapi_modules_installed($modules) {
  $potential_conflicts = [
    'content_translation',
    'config_translation',
    'language',
  ];
  if (!empty(array_intersect($modules, $potential_conflicts))) {
    \Drupal::messenger()->addWarning(t('Some multilingual features currently do not work well with WOT:API. See the <a href=":wotapi-docs">WOT:API multilingual support documentation</a> for more information on the current status of multilingual support.', [
      ':wotapi-docs' => 'https://www.drupal.org/docs/8/modules/wotapi/translations',
    ]));
  }
}

/**
 * Implements hook_entity_bundle_create().
 */
function wotapi_entity_bundle_create() {
  Routes::rebuild();
}

/**
 * Implements hook_entity_bundle_delete().
 */
function wotapi_entity_bundle_delete() {
  Routes::rebuild();
}

/**
 * Implements hook_entity_create().
 */
function wotapi_entity_create(EntityInterface $entity) {
  if (in_array($entity->getEntityTypeId(), ['field_storage_config', 'field_config'])) {
    // @todo: only do this when relationship fields are updated, not just any field.
    Routes::rebuild();
  }
}

/**
 * Implements hook_entity_delete().
 */
function wotapi_entity_delete(EntityInterface $entity) {
  if (in_array($entity->getEntityTypeId(), ['field_storage_config', 'field_config'])) {
    // @todo: only do this when relationship fields are updated, not just any field.
    Routes::rebuild();
  }
}

/**
 * Implements hook_wotapi_entity_filter_access().
 */
function wotapi_wotapi_entity_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // All core entity types and most or all contrib entity types allow users
  // with the entity type's administrative permission to view all of the
  // entities, so enable similarly permissive filtering to those users as well.
  // A contrib module may override this decision by returning
  // AccessResult::forbidden() from its implementation of this hook.
  if ($admin_permission = $entity_type->getAdminPermission()) {
    return ([
      WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, $admin_permission),
    ]);
  }
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'aggregator_feed'.
 */
function wotapi_wotapi_aggregator_feed_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\aggregator\FeedAccessControlHandler::checkAccess()
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'access news feeds'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'block_content'.
 */
function wotapi_wotapi_block_content_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess()
  // \Drupal\wotapi\Access\TemporaryQueryGuard adds the condition for
  // (isReusable()), so this does not have to.
  return ([
    WOTAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowed(),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'comment'.
 */
function wotapi_wotapi_comment_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\comment\CommentAccessControlHandler::checkAccess()
  // \Drupal\wotapi\Access\TemporaryQueryGuard adds the condition for
  // (access to the commented entity), so this does not have to.
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer comments'),
    WOTAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'access comments'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'entity_test'.
 */
function wotapi_wotapi_entity_test_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\entity_test\EntityTestAccessControlHandler::checkAccess()
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'view test entity'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'file'.
 */
function wotapi_wotapi_file_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\file\FileAccessControlHandler::checkAccess()
  // \Drupal\wotapi\Access\TemporaryQueryGuard adds the condition for
  // (public OR owner), so this does not have to.
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'access content'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'media'.
 */
function wotapi_wotapi_media_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\media\MediaAccessControlHandler::checkAccess()
  return ([
    WOTAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'view media'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'node'.
 */
function wotapi_wotapi_node_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\node\NodeAccessControlHandler::access()
  if ($account->hasPermission('bypass node access')) {
    return ([
      WOTAPI_FILTER_AMONG_ALL => AccessResult::allowed()->cachePerPermissions(),
    ]);
  }
  if (!$account->hasPermission('access content')) {
    $forbidden = AccessResult::forbidden("The 'access content' permission is required.")->cachePerPermissions();
    return ([
      WOTAPI_FILTER_AMONG_ALL => $forbidden,
      WOTAPI_FILTER_AMONG_OWN => $forbidden,
      WOTAPI_FILTER_AMONG_PUBLISHED => $forbidden,
      // For legacy reasons, the Node entity type has a "status" key, so forbid
      // this subset as well, even though it has no semantic meaning.
      WOTAPI_FILTER_AMONG_ENABLED => $forbidden,
    ]);
  }

  return ([
    // @see \Drupal\node\NodeAccessControlHandler::checkAccess()
    WOTAPI_FILTER_AMONG_OWN => AccessResult::allowedIfHasPermission($account, 'view own unpublished content'),

    // @see \Drupal\node\NodeGrantDatabaseStorage::access()
    // Note that:
    // - This is just for the default grant. Other node access conditions are
    //   added via the 'node_access' query tag.
    // - Permissions were checked earlier in this function, so we must vary the
    //   cache by them.
    WOTAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowed()->cachePerPermissions(),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'shortcut'.
 */
function wotapi_wotapi_shortcut_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\shortcut\ShortcutAccessControlHandler::checkAccess()
  // \Drupal\wotapi\Access\TemporaryQueryGuard adds the condition for
  // (shortcut_set = shortcut_current_displayed_set()), so this does not have
  // to.
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer shortcuts')
      ->orIf(AccessResult::allowedIfHasPermissions($account, ['access shortcuts', 'customize shortcut links'])),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'taxonomy_term'.
 */
function wotapi_wotapi_taxonomy_term_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\taxonomy\TermAccessControlHandler::checkAccess()
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'administer taxonomy'),
    WOTAPI_FILTER_AMONG_PUBLISHED => AccessResult::allowedIfHasPermission($account, 'access content'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'user'.
 */
function wotapi_wotapi_user_filter_access(EntityTypeInterface $entity_type, AccountInterface $account) {
  // @see \Drupal\user\UserAccessControlHandler::checkAccess()
  // \Drupal\wotapi\Access\TemporaryQueryGuard adds the condition for
  // (!isAnonymous()), so this does not have to.
  return ([
    WOTAPI_FILTER_AMONG_OWN => AccessResult::allowed(),
    WOTAPI_FILTER_AMONG_ENABLED => AccessResult::allowedIfHasPermission($account, 'access user profiles'),
  ]);
}

/**
 * Implements hook_wotapi_ENTITY_TYPE_filter_access() for 'workspace'.
 */
function wotapi_wotapi_workspace_filter_access(EntityTypeInterface $entity_type, $published, $owner, AccountInterface $account) {
  // @see \Drupal\workspaces\WorkspaceAccessControlHandler::checkAccess()
  // \Drupal\wotapi\Access\TemporaryQueryGuard adds the condition for
  // (isDefaultWorkspace()), so this does not have to.
  return ([
    WOTAPI_FILTER_AMONG_ALL => AccessResult::allowedIfHasPermission($account, 'view any workspace'),
    WOTAPI_FILTER_AMONG_OWN => AccessResult::allowedIfHasPermission($account, 'view own workspace'),
  ]);
}



/**
 * Implement hook_entity_presave().
 * Change the default widget form entity_reference to IEF if the target is Thing property.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 */
function wotapi_property_entity_presave(EntityInterface $entity) {
  if ($entity instanceof FieldConfigInterface && !$entity->isDeleted() && !$entity->isSyncing()) {
    /** @var \Drupal\field\FieldConfigInterface $entity */
    if ($entity->getType() === 'entity_reference' && $entity->getSetting('target_type') === 'wotapi_property') {
      // TODO don't hard code EntityFormDisplay type -- default.
      $display_form_id = $entity->getTargetEntityTypeId() . '.' . $entity->getTargetBundle() . '.default';
      $entity_form_display = EntityFormDisplay::load($display_form_id);
      // Returned if entity_form_display is not set.
      if (is_null($entity_form_display)) {
        return;
      }
      $config_copy = $entity_form_display->get('content');
      // If the widget is already IEF, then we don't override the settings at all.
      if ($config_copy[$entity->getName()]['type'] === 'inline_entity_form_simple') {
        return;
      }
      // Set IEF form.
      $config_copy[$entity->getName()]['type'] = 'inline_entity_form_simple';
      $config_copy[$entity->getName()]['settings'] = [
        'form_mode' => 'default',
        'override_labels' => FALSE,
        'label_singular' => '',
        'label_plural' => '',
      ];
      $entity_form_display->set('content', $config_copy);
      $entity_form_display->save();

      // Set rendered entity view for Thing property Entity.
      $entity_view_display = EntityViewDisplay::load($display_form_id);
      $config_copy = $entity_view_display->get('content');
      $config_copy[$entity->getName()]['type'] = 'entity_reference_entity_view';
      $config_copy[$entity->getName()]['settings']['view_mode'] = 'default';
      $entity_view_display->set('content', $config_copy);
      $entity_view_display->save();
    }
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc