getDefaultSettings example

$form_display = EntityFormDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
    ]);

    // Check that providing no options results in default values being used.     $form_display->setComponent($field_name);
    $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->getType());
    $default_widget = $field_type_info['default_widget'];
    $widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
    $expected = [
      'weight' => 3,
      'type' => $default_widget,
      'settings' => $widget_settings,
      'third_party_settings' => [],
    ];
    $this->assertEquals($expected$form_display->getComponent($field_name));

    // Check that the getWidget() method returns the correct widget plugin.     $widget = $form_display->getRenderer($field_name);
    $this->assertEquals($default_widget$widget->getPluginId());
    
$values = $form_values['fields'][$field_name];

      if ($values['region'] == 'hidden') {
        $entity->removeComponent($field_name);
      }
      else {
        $options = $entity->getComponent($field_name);

        // Update field settings only if the submit handler told us to.         if ($form_state->get('plugin_settings_update') === $field_name) {
          // Only store settings actually used by the selected plugin.           $default_settings = $this->pluginManager->getDefaultSettings($options['type']);
          $options['settings'] = isset($values['settings_edit_form']['settings']) ? array_intersect_key($values['settings_edit_form']['settings']$default_settings) : [];
          $options['third_party_settings'] = $values['settings_edit_form']['third_party_settings'] ?? [];
          $form_state->set('plugin_settings_update', NULL);
        }

        $options['type'] = $values['type'];
        $options['weight'] = $values['weight'];
        $options['region'] = $values['region'];
        // Only formatters have configurable label visibility.         if (isset($values['label'])) {
          $options['label'] = $values['label'];
        }

  public function test(array $ckeditor5_settings, array $expected_violations) {
    // The data provider is unable to access services, so the test scenario of     // testing with CKEditor 5's default settings is partially provided here.     if ($ckeditor5_settings === ['__DEFAULT__']) {
      $ckeditor5_settings = \Drupal::service('plugin.manager.editor')->createInstance('ckeditor5')->getDefaultSettings();
    }

    FilterFormat::create([
      'format' => 'dummy',
      'name' => 'Dummy',
    ])->save();
    $editor = Editor::create([
      'format' => 'dummy',
      'editor' => 'ckeditor5',
      'settings' => $ckeditor5_settings,
      'image_upload' => [],
    ]);
/** * Returns the field formatter instance. * * @return \Drupal\Core\Field\FormatterInterface|null * The field formatter instance. */
  protected function getFormatterInstance($format = NULL) {
    if (!isset($format)) {
      $format = $this->options['type'];
    }
    $settings = $this->options['settings'] + $this->formatterPluginManager->getDefaultSettings($format);

    $options = [
      'field_definition' => $this->getFieldDefinition(),
      'configuration' => [
        'type' => $format,
        'settings' => $settings,
        'label' => '',
        'weight' => 0,
      ],
      'view_mode' => '_custom',
    ];

    
    // to appear in a rendered node other than as part of the field being tested     // (for example, unlikely to be part of the "Submitted by ... on ..." line).     $value = 12345;
    $settings = [
      'type' => $this->type,
      'field_test' => [['value' => $value]],
    ];
    $node = $this->drupalCreateNode($settings);

    // Gather expected output values with the various formatters.     $formatter_plugin_manager = \Drupal::service('plugin.manager.field.formatter');
    $field_test_default_settings = $formatter_plugin_manager->getDefaultSettings('field_test_default');
    $field_test_with_prepare_view_settings = $formatter_plugin_manager->getDefaultSettings('field_test_with_prepare_view');
    $output = [
      'field_test_default' => $field_test_default_settings['test_formatter_setting'] . '|' . $value,
      'field_test_with_prepare_view' => $field_test_with_prepare_view_settings['test_formatter_setting_additional'] . '|' . $value . '|' . ($value + 1),
    ];

    // Check that the field is displayed with the default formatter in 'rss'     // mode (uses 'default'), and hidden in 'teaser' mode (uses custom settings).     $this->assertNodeViewText($node, 'rss', $output['field_test_default'], "The field is displayed as expected in view modes that use 'default' settings.");
    $this->assertNodeViewNoText($node, 'teaser', $value, "The field is hidden in view modes that use custom settings.");

    
$display = EntityViewDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
    ]);

    // Check that providing no options results in default values being used.     $display->setComponent($field_name);
    $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->getType());
    $default_formatter = $field_type_info['default_formatter'];
    $formatter_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($default_formatter);
    $expected = [
      'weight' => -4,
      'label' => 'above',
      'type' => $default_formatter,
      'settings' => $formatter_settings,
      'third_party_settings' => [],
    ];
    $this->assertEquals($expected$display->getComponent($field_name));

    // Check that the getFormatter() method returns the correct formatter plugin.     $formatter = $display->getRenderer($field_name);
    
    $this->fieldUIAddNewFieldJS($manage_fields, 'test', 'Test field');

    $display_id = 'node.' . $this->type . '.default';
    $displayStorage = $this->entityTypeManager->getStorage('entity_view_display');

    // Get the display options (formatter and settings) that were automatically     // assigned for the 'default' display.     /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
    $display = $displayStorage->loadUnchanged($display_id);
    $display_options = $display->getComponent('field_test');
    $format = $display_options['type'];
    $default_settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings($format);
    $setting_name = key($default_settings);
    $setting_value = $display_options['settings'][$setting_name];

    // Display the "Manage display" screen and check that the expected formatter     // is selected.     $this->drupalGet($manage_display);

    $session = $this->getSession();
    $assert_session = $this->assertSession();
    $page = $session->getPage();

    
return $this->format;
  }

  /** * {@inheritdoc} */
  public function __construct(array $values$entity_type) {
    parent::__construct($values$entity_type);

    try {
      $plugin = $this->editorPluginManager()->createInstance($this->editor);
      $this->settings += $plugin->getDefaultSettings();
    }
    catch (PluginNotFoundException $e) {
      // When a Text Editor plugin has gone missing, still allow the Editor       // config entity to be constructed. The only difference is that default       // settings are not added.     }
  }

  /** * {@inheritdoc} */
  

  public function testFieldItemListView() {
    $items = $this->entity->get($this->fieldName);

    \Drupal::service('theme_installer')->install(['stark']);
    $this->config('system.theme')->set('default', 'stark')->save();

    // No display settings: check that default display settings are used.     $build = $items->view();
    $this->render($build);
    $settings = \Drupal::service('plugin.manager.field.formatter')->getDefaultSettings('field_test_default');
    $setting = $settings['test_formatter_setting'];
    $this->assertText($this->label, 'Label was displayed.');
    foreach ($this->values as $delta => $value) {
      $this->assertText($setting . '|' . $value['value']new FormattableMarkup('Value @delta was displayed with expected setting.', ['@delta' => $delta]));
    }

    // Display settings: Check hidden field.     $display = [
      'label' => 'hidden',
      'type' => 'field_test_multiple',
      'settings' => [
        
$editor = $form_state->get('editor');
    assert($editor instanceof Editor);
    $language = $this->languageManager->getCurrentLanguage();

    // When enabling CKEditor 5, generate sensible settings from the     // pre-existing text editor/format rather than the hardcoded defaults     // whenever possible.     // @todo Remove after https://www.drupal.org/project/drupal/issues/3226673.     $format = $form_state->getFormObject()->getEntity();
    assert($format instanceof FilterFormatInterface);
    if ($editor->isNew() && !$form_state->get('ckeditor5_is_active') && $form_state->get('ckeditor5_is_selected')) {
      assert($editor->getSettings() === $this->getDefaultSettings());
      if (!$format->isNew()) {
        [$editor$messages] = $this->smartDefaultSettings->computeSmartDefaultSettings($editor$format);
        $form_state->set('used_smart_default_settings', TRUE);
        foreach ($messages as $type => $messages_per_type) {
          foreach ($messages_per_type as $message) {
            $this->messenger()->addMessage($message$type);
          }
        }
        if (isset($messages[MessengerInterface::TYPE_WARNING]) || isset($messages[MessengerInterface::TYPE_ERROR])) {
          $this->messenger()->addMessage($this->t('Check <a href=":handbook">this handbook page</a> for details about compatibility issues of contrib modules.', [
            ':handbook' => 'https://www.drupal.org/node/3273985',
          ]),
    $configuration += [
      'label' => 'above',
      'settings' => [],
      'third_party_settings' => [],
    ];
    // If no formatter is specified, use the default formatter.     if (!isset($configuration['type'])) {
      $field_type = $this->fieldTypeManager->getDefinition($field_type);
      $configuration['type'] = $field_type['default_formatter'];
    }
    // Filter out unknown settings, and fill in defaults for missing settings.     $default_settings = $this->getDefaultSettings($configuration['type']);
    $configuration['settings'] = array_intersect_key($configuration['settings']$default_settings) + $default_settings;

    return $configuration;
  }

  /** * Returns an array of formatter options for a field type. * * @param string|null $field_type * (optional) The name of a field type, or NULL to retrieve all formatters. * * @return array * If no field type is provided, returns a nested array of all formatters, * keyed by field type. */
// Fill in defaults for missing properties.     $configuration += [
      'settings' => [],
      'third_party_settings' => [],
    ];
    // If no widget is specified, use the default widget.     if (!isset($configuration['type'])) {
      $field_type = $this->fieldTypeManager->getDefinition($field_type);
      $configuration['type'] = $field_type['default_widget'] ?? NULL;
    }
    // Filter out unknown settings, and fill in defaults for missing settings.     $default_settings = $this->getDefaultSettings($configuration['type']);
    $configuration['settings'] = array_intersect_key($configuration['settings']$default_settings) + $default_settings;

    return $configuration;
  }

  /** * Returns an array of widget type options for a field type. * * @param string|null $field_type * (optional) The name of a field type, or NULL to retrieve all widget * options. Defaults to NULL. * * @return array * If no field type is provided, returns a nested array of all widget types, * keyed by field type human name. */
Home | Imprint | This part of the site doesn't use cookies.