Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By marcoka on
I am currently porting the amazon_pa module to drupal 8: https://www.drupal.org/project/amazon_pa
The widget has a custom setting called locale. Meaning it is a select box here: /admin/structure/types/manage/article/fields/node.article.field_amz/storage
I am not able to save the vale i input there. So what one has to do that the form element $element['locale'] is actually saved and can be retrived late?
Thank you very much.
<?php namespace Drupal\asin\Plugin\Field\FieldType; use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\TypedData\DataDefinition; use Drupal\Core\Form\FormStateInterface; /** * Plugin implementation of the 'asin' field type. * * @FieldType( * id = "field_asin", * label = @Translation("Amazon asin field"), * module = "asin", * description = @Translation("Amazon ASIN field"), * default_widget = "asin_text", * default_formatter = "asin_plain" * ) */ class AmazonField extends FieldItemBase { /** * {@inheritdoc} */ public static function schema(FieldStorageDefinitionInterface $field_definition) { $locale = $field_definition->getSetting('locale'); return array( 'columns' => array( 'asin' => array( 'type' => 'varchar', 'length' => 32, 'not null' => TRUE, ) ) ); } /** * {@inheritdoc} */ public function isEmpty() { $value = $this->get('asin')->getValue(); return $value === NULL || $value === ''; } /** * {@inheritdoc} */ public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['asin'] = DataDefinition::create('string') ->setLabel(t('ASIN value')); return $properties; } /** * {@inheritdoc} * * Add locale settings to a field */ public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) { $element = []; $settings = $this->getSettings(); $Utils = New \Drupal\amazon_pa\Utils\AmazonPaUtils(); $cache= $Utils->amazon_pa_data_cache(FALSE); $config = \Drupal::config('amazon_pa.settings'); $locale_options = array('' => '-- Select --'); foreach ($cache['locales'] as $locale => $data) { if ($config->get('amazon_locale_' . $locale . '_associate_id', '')) { $locale_options[$locale] = $data['name']; } } $stop=1; $locale = $this->getSetting('locale'); $element['locale'] = array( '#title' => t('Amazon Locale'), '#type' => 'select', '#options' => $locale_options, '#default_value' => $this->getSetting('locale'), '#description' => t('Madatory locale'), '#disabled' => $has_data, '#required' => TRUE, return $element + parent::storageSettingsForm($form, $form_state, $has_data); } }
Comments
I got it but can not tell
I got it but can not tell what i did wrong exactly. I used fieldSettings to add form elements. You can see the result(asin submodule) if you want to see it when i upload the D8 Version at https://www.drupal.org/project/amazon_pa