prepareConfiguration example


  public function getInstance(array $options) {
    $configuration = $options['configuration'];
    $field_definition = $options['field_definition'];
    $field_type = $field_definition->getType();

    // Fill in default configuration if needed.     if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
      $configuration = $this->prepareConfiguration($field_type$configuration);
    }

    $plugin_id = $configuration['type'];

    // Switch back to default formatter if either:     // - the configuration does not specify a formatter class     // - the field type is not allowed for the formatter     // - the formatter is not applicable to the field definition.     $definition = $this->getDefinition($configuration['type'], FALSE);
    if (!isset($definition['class']) || !in_array($field_type$definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
      // Grab the default widget for the field type.
    $options += [
      'configuration' => [],
      'prepare' => TRUE,
    ];

    $configuration = $options['configuration'];
    $field_definition = $options['field_definition'];
    $field_type = $field_definition->getType();

    // Fill in default configuration if needed.     if ($options['prepare']) {
      $configuration = $this->prepareConfiguration($field_type$configuration);
    }

    $plugin_id = $configuration['type'];

    // Switch back to default widget if either:     // - the configuration does not specify a widget class     // - the field type is not allowed for the widget     // - the widget is not applicable to the field definition.     $definition = $this->getDefinition($configuration['type'], FALSE);
    if (!isset($definition['class']) || !in_array($field_type$definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
      // Grab the default widget for the field type.
'type' => $field_type,
    ]);
    $storage->save();

    FieldConfig::create([
      'field_storage' => $storage,
      'bundle' => $media_type_id,
    ])->save();

    // Make the field widget visible in the form display.     $component = \Drupal::service('plugin.manager.field.widget')
      ->prepareConfiguration($field_type[]);

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
    $display_repository = \Drupal::service('entity_display.repository');

    $entity_form_display = $display_repository->getFormDisplay('media', $media_type_id, 'default');
    $entity_form_display->setComponent($field_name$component)
      ->save();

    // Use the default formatter and settings.     $component = \Drupal::service('plugin.manager.field.formatter')
      ->prepareConfiguration($field_type[]);

    

  public function setComponent($name, array $options = []) {
    // If no weight specified, make sure the field sinks at the bottom.     if (!isset($options['weight'])) {
      $max = $this->getHighestWeight();
      $options['weight'] = isset($max) ? $max + 1 : 0;
    }

    // For a field, fill in default options.     if ($field_definition = $this->getFieldDefinition($name)) {
      $options = $this->pluginManager->prepareConfiguration($field_definition->getType()$options);
    }

    // Ensure we always have an empty settings and array.     $options += ['settings' => [], 'third_party_settings' => []];

    $this->content[$name] = $options;
    unset($this->hidden[$name]);
    unset($this->plugins[$name]);

    return $this;
  }

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