onFieldDefinitionCreate example

/** * @covers ::onFieldDefinitionCreate */
  public function testOnFieldDefinitionCreateNewField() {
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getTargetEntityTypeId()->willReturn('test_entity_type');
    $field_definition->getTargetBundle()->willReturn('test_bundle');
    $field_definition->getName()->willReturn('test_field');
    $field_definition->getType()->willReturn('test_type');

    $storage = $this->prophesize(DynamicallyFieldableEntityStorageInterface::class);
    $storage->onFieldDefinitionCreate($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]);

    // Set up the stored bundle field map.     $key_value_store = $this->prophesize(KeyValueStoreInterface::class);
    $this->keyValueFactory->get('entity.definitions.bundle_field_map')->willReturn($key_value_store->reveal());
    $key_value_store->get('test_entity_type')->willReturn([]);
    $key_value_store->set('test_entity_type', [
      'test_field' => [
        
$storage_definition = $this->getFieldStorageDefinition();

    // Filter out unknown settings and make sure all settings are present, so     // that a complete field definition is passed to the various hooks and     // written to config.     $default_settings = $field_type_manager->getDefaultFieldSettings($storage_definition->getType());
    $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;

    if ($this->isNew()) {
      // Notify the entity storage.       \Drupal::service('field_definition.listener')->onFieldDefinitionCreate($this);
    }
    else {
      // 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.");
      }


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

    // Notify the storage about the new field.     $this->entityTypeManager->getStorage($entity_type_id)->onFieldDefinitionCreate($field_definition);

    // Update the bundle field map key value collection, add the new field.     $bundle_field_map = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->get($entity_type_id);
    if (!isset($bundle_field_map[$field_name])) {
      // This field did not exist yet, initialize it with the type and empty       // bundle list.       $bundle_field_map[$field_name] = [
        'type' => $field_definition->getType(),
        'bundles' => [],
      ];
    }
    
Home | Imprint | This part of the site doesn't use cookies.