getDefaultStorageSettings example


  protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $field_definition_id = 'id', $entity_keys = []) {
    $field_type_manager = $this->prophesize(FieldTypePluginManagerInterface::class);
    $field_type_manager->getDefaultStorageSettings('boolean')->willReturn([]);
    $field_type_manager->getDefaultFieldSettings('boolean')->willReturn([]);
    $this->container->get('plugin.manager.field.field_type')->willReturn($field_type_manager->reveal());

    $string_translation = $this->prophesize(TranslationInterface::class);
    $this->container->get('string_translation')->willReturn($string_translation->reveal());

    $entity_class = EntityTypeManagerTestEntity::class;

    $field_definition = $this->prophesize()->willImplement(FieldDefinitionInterface::class)->willImplement(FieldStorageDefinitionInterface::class);
    $entity_class::$baseFieldDefinitions = [
      $field_definition_id => $field_definition->reveal(),
    ];
// Ensure that basic properties are preserved.     $this->assertEquals($field_storage_definition['field_name']$field_storage_config['field_name'], 'The field name is properly saved.');
    $this->assertEquals($field_storage_definition['entity_type']$field_storage_config['entity_type'], 'The field entity type is properly saved.');
    $this->assertEquals($field_storage_definition['entity_type'] . '.' . $field_storage_definition['field_name']$field_storage_config['id'], 'The field id is properly saved.');
    $this->assertEquals($field_storage_definition['type']$field_storage_config['type'], 'The field type is properly saved.');

    // Ensure that cardinality defaults to 1.     $this->assertEquals(1, $field_storage_config['cardinality'], 'Cardinality defaults to 1.');

    // Ensure that default settings are present.     $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $this->assertEquals($field_type_manager->getDefaultStorageSettings($field_storage_definition['type'])$field_storage_config['settings'], 'Default storage settings have been written.');

    // Guarantee that the name is unique.     try {
      FieldStorageConfig::create($field_storage_definition)->save();
      $this->fail('Cannot create two fields with the same name.');
    }
    catch (\Exception $e) {
      $this->assertInstanceOf(EntityStorageException::class$e);
    }

    // Check that field type is required.

  public static function create($type) {
    $field_definition = new static([]);
    $field_definition->type = $type;
    $field_definition->itemDefinition = FieldItemDataDefinition::create($field_definition);
    // Create a definition for the items, and initialize it with the default     // settings for the field type.     $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $default_settings = $field_type_manager->getDefaultStorageSettings($type) + $field_type_manager->getDefaultFieldSettings($type);
    $field_definition->itemDefinition->setSettings($default_settings);
    return $field_definition;
  }

  /** * Creates a new field definition based upon a field storage definition. * * In cases where one needs a field storage definitions to act like full * field definitions, this creates a new field definition based upon the * (limited) information available. That way it is possible to use the field * definition in places where a full field definition is required; e.g., with * widgets or formatters. * * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $definition * The field storage definition to base the new field definition upon. * * @return $this */

class FieldTypePluginManagerTest extends FieldKernelTestBase {

  /** * Tests the default settings convenience methods. */
  public function testDefaultSettings() {
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    foreach (['test_field', 'shape', 'hidden_test_field'] as $type) {
      $definition = $field_type_manager->getDefinition($type);
      $this->assertSame($field_type_manager->getDefaultStorageSettings($type)$definition['class']::defaultStorageSettings()new FormattableMarkup("%type storage settings were returned", ['%type' => $type]));
      $this->assertSame($field_type_manager->getDefaultFieldSettings($type)$definition['class']::defaultFieldSettings()new FormattableMarkup(" %type field settings were returned", ['%type' => $type]));
    }
  }

  /** * Tests creation of field item instances. */
  public function testCreateInstance() {
    /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    foreach (['test_field', 'shape', 'hidden_test_field'] as $type) {
      
'field_settings' => [
        'some_instance_setting' => 'value 2',
      ],
      'storage_settings' => [
        'some_storage_setting' => 'some value',
      ],
    ];
    $field_type_manager = $this->prophesize(FieldTypePluginManagerInterface::class);
    $field_type_manager->getDefinitions()->willReturn([$this->fieldType => $this->fieldTypeDefinition]);
    $field_type_manager->getDefinition()->willReturn($this->fieldTypeDefinition);
    $field_type_manager->getDefaultFieldSettings($this->fieldType)->willReturn($this->fieldTypeDefinition['field_settings']);
    $field_type_manager->getDefaultStorageSettings($this->fieldType)->willReturn($this->fieldTypeDefinition['storage_settings']);

    $storage_definition = $this->prophesize(FieldStorageDefinitionInterface::class);
    $storage_definition->getMainPropertyName()->willReturn('value');
    $storage_definition->getType()->willReturn($this->fieldType);
    $storage_definition->getName()->willReturn('test_field_name');
    $storage_supports_translation = &$this->storageSupportsTranslation;
    $storage_definition->isTranslatable()->will(function D) use (&$storage_supports_translation) {
      return $storage_supports_translation;
    });
    $storage_definition->getSettings()->willReturn($this->fieldTypeDefinition['storage_settings']);
    $storage_definition->getSetting('some_storage_setting')->willReturn($this->fieldTypeDefinition['storage_settings']['some_storage_setting']);

    

  public function preSave(EntityStorageInterface $storage) {
    // Clear the derived data about the field.     unset($this->schema);

    // 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.     $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    $default_settings = $field_type_manager->getDefaultStorageSettings($this->type);
    $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;

    if ($this->isNew()) {
      $this->preSaveNew($storage);
    }
    else {
      $this->preSaveUpdated($storage);
    }

    parent::preSave($storage);
  }

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