getDefaultFieldSettings example


  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 */

  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(),
    ];
    
/** * {@inheritdoc} * * @throws \Drupal\Core\Field\FieldException * If the bundle is being changed. */
  public function preSave(EntityStorageInterface $storage) {
    // 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->getDefaultFieldSettings($this->getType());
    $this->settings = array_intersect_key($this->settings, $default_settings) + $default_settings;

    // Call the parent's presave method to perform validate and calculate     // dependencies.     parent::preSave($storage);

    if ($this->isNew()) {
      // @todo This assumes that the previous definition isn't some       // non-config-based override, but that might not be the case:       // https://www.drupal.org/node/2321071.       $previous_definition = $this->getBaseFieldDefinition();
    }

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) {
      $definition = $field_type_manager->getDefinition($type);

      

  public function preSave(EntityStorageInterface $storage) {
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');

    $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.");
      }
      

  public function setFieldStorageDefinition(FieldStorageDefinitionInterface $storageDefinition) {
    $this->fieldStorageDefinition = $storageDefinition;
    $this->itemDefinition = FieldItemDataDefinition::create($this);
    // 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->getDefaultFieldSettings($storageDefinition->getType());
    $this->itemDefinition->setSettings($default_settings);
    return $this;
  }

  /** * {@inheritdoc} */
  public function getFieldStorageDefinition() {
    return $this->fieldStorageDefinition;
  }

  
'id' => $this->fieldType,
      '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']);
    
// Since we are working with raw configuration, this needs to be unset     // manually.     // @see Drupal\field_test\Plugin\Field\FieldType\TestItem::fieldSettingsFromConfigData()     unset($config['settings']['config_data_from_field_setting']);

    // Check that default values are set.     $this->assertFalse($config['required'], 'Required defaults to false.');
    $this->assertSame($config['label']$this->fieldDefinition['field_name'], 'Label defaults to field name.');
    $this->assertSame('', $config['description'], 'Description defaults to empty string.');

    // Check that default settings are set.     $this->assertEquals($config['settings']$field_type_manager->getDefaultFieldSettings($this->fieldStorageDefinition['type']), 'Default field settings have been written.');

    // Check that the denormalized 'field_type' was properly written.     $this->assertEquals($config['field_type']$this->fieldStorageDefinition['type']);

    // Guarantee that the field/bundle combination is unique.     try {
      FieldConfig::create($this->fieldDefinition)->save();
      $this->fail('Cannot create two fields with the same field / bundle combination.');
    }
    catch (EntityStorageException $e) {
      // Expected exception; just continue testing.


  /** * {@inheritdoc} */
  public function createSourceField(MediaTypeInterface $type) {
    /** @var \Drupal\field\FieldConfigInterface $field */
    $field = parent::createSourceField($type);

    // Reset the field to its default settings so that we don't inherit the     // settings from the parent class' source field.     $settings = $this->fieldTypeManager->getDefaultFieldSettings($field->getType());

    return $field->set('settings', $settings);
  }

  /** * {@inheritdoc} */
  public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
    parent::prepareViewDisplay($type$display);

    // Use the `large` image style and do not link the image to anything.
Home | Imprint | This part of the site doesn't use cookies.