You are here

search_api_exclude_entity.module in Search API Exclude Entity 8

File

search_api_exclude_entity.module
View source
<?php

/**
 * @file
 * Contains search_api_exclude_entity.module.
 */
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Access\AccessResult;

/**
 * Implements hook_form_FORM_ID_alter() for 'field_storage_config_edit_form'.
 */
function search_api_exclude_entity_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state) {
  if ($form_state
    ->getFormObject()
    ->getEntity()
    ->getType() == 'search_api_exclude_entity') {

    // Hide the cardinality field.
    $form['cardinality_container']['#access'] = FALSE;
    $form['cardinality_container']['#disabled'] = TRUE;
  }
}

/**
 * Implements hook_entity_field_access().
 */
function search_api_exclude_entity_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($field_definition
    ->getType() == 'search_api_exclude_entity' && $operation == 'edit') {
    if (!$account
      ->hasPermission('administer search api exclude entity')) {
      return AccessResult::forbidden();
    }
  }
  return AccessResult::neutral();
}