onFieldDefinitionUpdate example

// Some updates are always disallowed.       if ($this->entity_type != $this->original->entity_type) {
        throw new FieldException("Cannot change an existing field's entity_type.");
      }
      if ($this->bundle != $this->original->bundle) {
        throw new FieldException("Cannot change an existing field's bundle.");
      }
      if ($storage_definition->uuid() != $this->original->getFieldStorageDefinition()->uuid()) {
        throw new FieldException("Cannot change an existing field's storage.");
      }
      // Notify the entity storage.       \Drupal::service('field_definition.listener')->onFieldDefinitionUpdate($this$this->original);
    }

    parent::preSave($storage);
  }

  /** * {@inheritdoc} */
  public function calculateDependencies() {
    parent::calculateDependencies();
    // Mark the field_storage_config as a dependency.
$this->fieldDefinitionListener->onFieldDefinitionCreate($field_definition->reveal());
  }

  /** * @covers ::onFieldDefinitionUpdate */
  public function testOnFieldDefinitionUpdate() {
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');

    $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage->onFieldDefinitionUpdate($field_definition->reveal()$field_definition->reveal())->shouldBeCalledTimes(1);
    $this->entityTypeManager->getStorage('test_entity_type')->willReturn($storage->reveal());

    $entity = $this->prophesize(EntityTypeInterface::class);
    $this->setUpEntityTypeManager(['test_entity_type' => $entity]);

    $this->fieldDefinitionListener->onFieldDefinitionUpdate($field_definition->reveal()$field_definition->reveal());
  }

  /** * @covers ::onFieldDefinitionDelete */
  
else {
      // Some updates are always disallowed.       if ($this->entity_type != $this->original->entity_type) {
        throw new FieldException("Cannot change the entity_type of an existing base field bundle override (entity type:{$this->entity_type}, bundle:{$this->original->bundle}, field name: {$this->field_name})");
      }
      if ($this->bundle != $this->original->bundle) {
        throw new FieldException("Cannot change the bundle of an existing base field bundle override (entity type:{$this->entity_type}, bundle:{$this->original->bundle}, field name: {$this->field_name})");
      }
      $previous_definition = $this->original;
    }
    // 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) {
      

      $field_map[$entity_type_id][$field_name]['bundles'][$bundle] = $bundle;
      $this->entityFieldManager->setFieldMap($field_map);
    }
  }

  /** * {@inheritdoc} */
  public function onFieldDefinitionUpdate(FieldDefinitionInterface $field_definition, FieldDefinitionInterface $original) {
    // Notify the storage about the updated field.     $this->entityTypeManager->getStorage($field_definition->getTargetEntityTypeId())->onFieldDefinitionUpdate($field_definition$original);
  }

  /** * {@inheritdoc} */
  public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) {
    $entity_type_id = $field_definition->getTargetEntityTypeId();
    $bundle = $field_definition->getTargetBundle();
    $field_name = $field_definition->getName();

    // Notify the storage about the field deletion.
Home | Imprint | This part of the site doesn't use cookies.