class FieldTestFieldHooks

Hook implementations for field_test.

Hierarchy

Expanded class hierarchy of FieldTestFieldHooks

File

core/modules/field/tests/modules/field_test/src/Hook/FieldTestFieldHooks.php, line 19

Namespace

Drupal\field_test\Hook
View source
class FieldTestFieldHooks {
  
  /**
   * Implements hook_field_widget_info_alter().
   */
  public function fieldWidgetInfoAlter(&$info) : void {
    $info['test_field_widget_multiple']['field_types'][] = 'test_field';
    $info['test_field_widget_multiple']['field_types'][] = 'test_field_with_preconfigured_options';
    // Add extra widget when needed for tests.
    // @see \Drupal\field\Tests\FormTest::widgetAlterTest().
    if ($alter_info = \Drupal::state()->get("field_test.widget_alter_test")) {
      if ($alter_info['widget'] === 'test_field_widget_multiple_single_value') {
        $info['test_field_widget_multiple_single_value']['field_types'][] = 'test_field';
      }
    }
  }
  
  /**
   * Implements hook_field_storage_config_update_forbid().
   */
  public function fieldStorageConfigUpdateForbid(FieldStorageConfigInterface $field_storage, FieldStorageConfigInterface $prior_field_storage) : void {
    if ($field_storage->getType() == 'test_field' && $field_storage->getSetting('unchangeable') != $prior_field_storage->getSetting('unchangeable')) {
      throw new FieldStorageDefinitionUpdateForbiddenException("field_test 'unchangeable' setting cannot be changed'");
    }
  }
  
  /**
   * Implements hook_entity_field_access().
   */
  public function entityFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, ?FieldItemListInterface $items = NULL) : AccessResultInterface {
    if ($field_definition->getName() == "field_no_{$operation}_access") {
      return AccessResult::forbidden();
    }
    // Only grant view access to test_view_field fields when the user has
    // 'view test_view_field content' permission.
    if ($field_definition->getName() == 'test_view_field' && $operation == 'view') {
      return AccessResult::forbiddenIf(!$account->hasPermission('view test_view_field content'))
        ->cachePerPermissions();
    }
    return AccessResult::allowed();
  }

}

Members

Title Sort descending Modifiers Object type Summary
FieldTestFieldHooks::entityFieldAccess public function Implements hook_entity_field_access().
FieldTestFieldHooks::fieldStorageConfigUpdateForbid public function Implements hook_field_storage_config_update_forbid().
FieldTestFieldHooks::fieldWidgetInfoAlter public function Implements hook_field_widget_info_alter().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.