clearCachedFieldDefinitions example


    // Notify the entity storage.     $this->entityTypeManager()->getStorage($this->getTargetEntityTypeId())->onFieldDefinitionUpdate($this$previous_definition);
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $field_overrides) {
    $entity_type_manager = \Drupal::entityTypeManager();
    // Clear the cache upfront, to refresh the results of getBundles().     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    /** @var \Drupal\Core\Field\Entity\BaseFieldOverride $field_override */
    foreach ($field_overrides as $field_override) {
      // Inform the system that the field definition is being updated back to       // its non-overridden state.       // @todo This assumes that there isn't a non-config-based override that       // we're returning to, but that might not be the case:       // https://www.drupal.org/node/2321071.       $entity_type_manager->getStorage($field_override->getTargetEntityTypeId())->onFieldDefinitionUpdate($field_override->getBaseFieldDefinition()$field_override);
    }
  }

  

  protected function applyEntityUpdates($entity_type_id = NULL) {
    $complete_change_list = \Drupal::entityDefinitionUpdateManager()->getChangeList();
    if ($complete_change_list) {
      // In case there are changes, explicitly invalidate caches.       \Drupal::entityTypeManager()->clearCachedDefinitions();
      \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    }

    if ($entity_type_id) {
      $complete_change_list = array_intersect_key($complete_change_list[$entity_type_id => TRUE]);
    }

    foreach ($complete_change_list as $entity_type_id => $change_list) {
      // Process entity type definition changes before storage definitions ones       // this is necessary when you change an entity type from non-revisionable       // to revisionable and at the same time add revisionable fields to the       // entity type.
// Configure one field as untranslatable.     $this->drupalLogin($this->administrator);
    $edit = [
      'settings[' . $this->entityTypeId . '][' . $this->bundle . '][fields][' . $this->fieldName . ']' => 0,
    ];
    $this->drupalGet('admin/config/regional/content-language');
    $this->submitForm($edit, 'Save configuration');

    /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
    $entity_field_manager = $this->container->get('entity_field.manager');
    $entity_field_manager->clearCachedFieldDefinitions();
    $definitions = $entity_field_manager->getFieldDefinitions($this->entityTypeId, $this->bundle);
    $this->assertFalse($definitions[$this->fieldName]->isTranslatable());
  }

  /** * {@inheritdoc} */
  protected function setupTestFields() {
    parent::setupTestFields();

    $field_storage = FieldStorageConfig::create([
      
// Update workflow options.     // @todo Make it possible to get default values without an entity.     // https://www.drupal.org/node/2318187     $node = $this->entityTypeManager->getStorage('node')->create(['type' => $type->id()]);
    foreach (['status', 'promote', 'sticky'] as $field_name) {
      $value = (bool) $form_state->getValue(['options', $field_name]);
      if ($node->$field_name->value != $value) {
        $fields[$field_name]->getConfig($type->id())->setDefaultValue($value)->save();
      }
    }

    $this->entityFieldManager->clearCachedFieldDefinitions();
    $form_state->setRedirectUrl($type->toUrl('collection'));
  }

}
    // from the field storage, so that it gets saved in the config record.     if (empty($this->field_type)) {
      $this->field_type = $this->getFieldStorageDefinition()->getType();
    }
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $fields) {
    // Clear the cache upfront, to refresh the results of getBundles().     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Notify the entity storage.     foreach ($fields as $field) {
      if (!$field->deleted) {
        \Drupal::service('field_definition.listener')->onFieldDefinitionDelete($field);
      }
    }
  }

  /** * {@inheritdoc} */
$field->save();
    $field_name = $this->fieldStorage->getName();

    // Test that constraints are applied to configurable fields. A TestField and     // a Range constraint are added dynamically to limit the field to values     // between 0 and 32.     // @see field_test_entity_bundle_field_info_alter()     \Drupal::state()->set('field_test_constraint', $field_name);

    // Clear the field definitions cache so the new constraints added by     // field_test_entity_bundle_field_info_alter() are taken into consideration.     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Test the newly added property constraints in the same request as when the     // caches were cleared. This will test the field definitions that are stored     // in the static cache of     // \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions().     $this->doFieldPropertyConstraintsTests();

    // In order to test a real-world scenario where the property constraints are     // only stored in the persistent cache of     // \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions(), we need to     // simulate a new request by removing the 'entity_field.manager' service,
$display_repository = \Drupal::service('entity_display.repository');

    // Change the widget to a datelist widget.     $display_repository->getFormDisplay($this->field->getTargetEntityTypeId()$this->field->getTargetBundle())
      ->setComponent($field_name[
        'type' => 'daterange_datelist',
        'settings' => [
          'date_order' => 'YMD',
        ],
      ])
      ->save();
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Display creation form.     $this->drupalGet('entity_test/add');
    $this->assertSession()->elementTextContains('xpath', '//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label);
    $this->assertSession()->elementExists('xpath', '//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]');
    $this->assertSession()->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0--description"]');

    // Assert that Hour and Minute Elements do not appear on Date Only.     $this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-value-hour\"]");
    $this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-value-minute\"]");
    $this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-end-value-hour\"]");
    
/** * Tests removing newly added extra field. */
  public function testNewExtraField() {
    $assert_session = $this->assertSession();
    $page = $this->getSession()->getPage();

    // At this point layout builder has been enabled for the test content type.     // Install a test module that creates a new extra field then clear cache.     \Drupal::service('module_installer')->install(['layout_builder_extra_field_test']);
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // View the layout and try to remove the new extra field.     $this->drupalGet(static::FIELD_UI_PREFIX . '/display/default/layout');
    $assert_session->pageTextContains('New Extra Field');
    $this->clickContextualLink('.block-extra-field-blocknodebundle-with-section-fieldlayout-builder-extra-field-test', 'Remove block');
    $this->assertNotEmpty($assert_session->waitForElementVisible('css', '#drupal-off-canvas'));
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->pageTextContains('Are you sure you want to remove');
    $page->pressButton('Remove');
    $assert_session->assertWaitOnAjaxRequest();
    $assert_session->pageTextNotContains('New Extra Field');
  }

  protected function enableContentModeration() {
    $this->drupalLogin($this->rootUser);
    $workflow_id = 'editorial';
    $this->drupalGet('/admin/config/workflow/workflows');
    $edit['bundles[' . $this->bundle . ']'] = TRUE;
    $this->drupalGet('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/' . $this->entityTypeId);
    $this->submitForm($edit, 'Save');
    // Ensure the parent environment is up-to-date.     // @see content_moderation_workflow_insert()     \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
    $router_builder = $this->container->get('router.builder');
    $router_builder->rebuildIfNeeded();
  }

  /** * {@inheritdoc} */
  protected function getEditorPermissions() {
    $editor_permissions = [
      "edit any {$this->bundle} content",
      "
'Changed the content type of 1 post from %old-type to %type.',
          'Changed the content type of @count posts from %old-type to %type.',
          [
            '%old-type' => $this->getOriginalId(),
            '%type' => $this->id(),
          ]));
      }
    }
    if ($update) {
      // Clear the cached field definitions as some settings affect the field       // definitions.       \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
    }
  }

  /** * {@inheritdoc} */
  public static function postDelete(EntityStorageInterface $storage, array $entities) {
    parent::postDelete($storage$entities);

    // Clear the node type cache to reflect the removal.     $storage->resetCache(array_keys($entities));
  }
parent::calculateDependencies();
    $target_entity_type = \Drupal::entityTypeManager()->getDefinition($this->targetEntityType);
    $this->addDependency('module', $target_entity_type->getProvider());
    return $this;
  }

  /** * {@inheritdoc} */
  public function preSave(EntityStorageInterface $storage) {
    parent::preSave($storage);
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  }

  /** * {@inheritdoc} */
  public static function preDelete(EntityStorageInterface $storage, array $entities) {
    parent::preDelete($storage$entities);
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
  }

  /** * {@inheritdoc} */

  public function onBundleCreate($bundle$entity_type_id) {
    $this->entityTypeBundleInfo->clearCachedBundles();
    // Notify the entity storage.     $storage = $this->entityTypeManager->getStorage($entity_type_id);
    if ($storage instanceof EntityBundleListenerInterface) {
      $storage->onBundleCreate($bundle$entity_type_id);
    }
    // Invoke hook_entity_bundle_create() hook.     $this->moduleHandler->invokeAll('entity_bundle_create', [$entity_type_id$bundle]);
    $this->entityFieldManager->clearCachedFieldDefinitions();
  }

  /** * {@inheritdoc} */
  public function onBundleDelete($bundle$entity_type_id) {
    $this->entityTypeBundleInfo->clearCachedBundles();
    // Notify the entity storage.     $storage = $this->entityTypeManager->getStorage($entity_type_id);
    if ($storage instanceof EntityBundleListenerInterface) {
      $storage->onBundleDelete($bundle$entity_type_id);
    }

  protected $defaultTheme = 'stark';

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    // @todo Figure why field definitions have to cleared in     // https://www.drupal.org/project/drupal/issues/2985882.     $this->container->get('entity_field.manager')->clearCachedFieldDefinitions();
  }

  /** * Tests that the layout override field is not normalized. */
  public function testOverrideField() {
    $this->assertCount(1, $this->node->get(OverridesSectionStorage::FIELD_NAME));

    // Make a GET request and ensure override field is not included.     $response = $this->request(
      'GET',
      


  /** * Check that field translatability is handled properly. */
  public function testFieldDefinitions() {
    // Check that field translatability can be altered to be enabled or disabled     // in field definitions.     $entity_type = 'entity_test_mulrev';
    $this->state->set('entity_test.field_definitions.translatable', ['name' => FALSE]);
    $entity_field_manager = \Drupal::service('entity_field.manager');
    $entity_field_manager->clearCachedFieldDefinitions();
    $definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type);
    $this->assertFalse($definitions['name']->isTranslatable(), 'Field translatability can be disabled programmatically.');

    $this->state->set('entity_test.field_definitions.translatable', ['name' => TRUE]);
    $entity_field_manager->clearCachedFieldDefinitions();
    $definitions = $entity_field_manager->getBaseFieldDefinitions($entity_type);
    $this->assertTrue($definitions['name']->isTranslatable(), 'Field translatability can be enabled programmatically.');

    // Check that field translatability is disabled by default.     $base_field_definitions = EntityTestMulRev::baseFieldDefinitions($this->entityTypeManager->getDefinition($entity_type));
    $this->assertTrue(!isset($base_field_definitions['id']->translatable), 'Translatability for the <em>id</em> field is not defined.');
    
    $this->drupalGet('node/' . $node->id() . '/delete');
    $this->submitForm([], 'Delete');
    $this->assertSession()->pageTextContains('The Moderated content moderated content has been deleted.');

    // Disable content moderation.     $edit['bundles[moderated_content]'] = FALSE;
    $this->drupalGet('admin/config/workflow/workflows/manage/editorial/type/node');
    $this->submitForm($edit, 'Save');
    // Ensure the parent environment is up-to-date.     // @see content_moderation_workflow_insert()     \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
    \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();

    // Create a new node.     $this->drupalGet('node/add/moderated_content');
    $this->submitForm(['title[0][value]' => 'non-moderated content'], 'Save');

    $node = $this->getNodeByTitle('non-moderated content');
    if (!$node) {
      $this->fail('Non-moderated test node was not saved correctly.');
    }
    $this->assertFalse($node->hasField('moderation_state'));
  }

  
Home | Imprint | This part of the site doesn't use cookies.