You are here

function mass_contact_entity_field_access in Mass Contact 8

Implements hook_entity_field_access().

File

./mass_contact.module, line 109
This is the main code file for the Mass Contact module.

Code

function mass_contact_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  if ($field_definition
    ->getName() === MassContactInterface::OPT_OUT_FIELD_ID) {

    // Only allow access if there are any categories, and site is configured
    // to allow opt-out.
    if (\Drupal::config('mass_contact.settings')
      ->get('optout_enabled') === MassContactInterface::OPT_OUT_DISABLED) {
      return AccessResultForbidden::forbidden('Opt out is disabled');
    }
    if (\Drupal::config('mass_contact.settings')
      ->get('optout_enabled') === MassContactInterface::OPT_OUT_CATEGORY && empty(\Drupal::entityTypeManager()
      ->getStorage('mass_contact_category')
      ->loadMultiple())) {
      return AccessResultForbidden::forbidden('Opt out is setup to allow configuration by categories, but no categories were found');
    }
  }
  return AccessResult::neutral();
}