You are here

public function LanguageItem::storageSettingsForm in Custom Language field 8

Returns a form for the storage-level settings.

Invoked from \Drupal\field_ui\Form\FieldStorageConfigEditForm to allow administrators to configure storage-level settings.

Field storage might reject settings changes that affect the field storage schema if the storage already has data. When the $has_data parameter is TRUE, the form should not allow changing the settings that take part in the schema() method. It is recommended to set #access to FALSE on the corresponding elements.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The form state of the (entire) configuration form.

bool $has_data: TRUE if the field already has data, FALSE if not.

Return value

array The form definition for the field settings.

Overrides FieldItemBase::storageSettingsForm

File

src/Plugin/Field/FieldType/LanguageItem.php, line 151

Class

LanguageItem
Plugin implementation of the 'language' field type.

Namespace

Drupal\languagefield\Plugin\Field\FieldType

Code

public function storageSettingsForm(array &$form, FormStateInterface $form_state, $has_data) {
  $element = parent::storageSettingsForm($form, $form_state, $has_data);
  $settings = $this
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getSettings();
  $languages = $this
    ->getPossibleOptions();
  $url_1 = \Drupal::moduleHandler()
    ->moduleExists('language') ? Url::fromRoute('entity.configurable_language.collection', [], [])
    ->toString() : '';
  $url_2 = Url::fromRoute('languagefield.custom_language.collection', [], [])
    ->toString();
  $element['language_range'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Enabled languages'),
    '#description' => $this
      ->t("Installed languages can be maintained on the\n        <a href=':url_1'>Languages</a> page, when Language module is installed. Custom languages can\n        be maintained on the <a href=':url_2'>Custom Languages</a> page. (Options marked with '*' are\n        typically used as default value in a hidden widget.)", [
      ':url_1' => $url_1,
      ':url_2' => $url_2,
    ]),
    '#required' => TRUE,
    '#default_value' => $settings['language_range'],
    '#options' => [
      // The following are from Languagefield.
      CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_PREDEFINED => $this
        ->t('All predefined languages'),
      // self::LANGUAGEFIELD_LANGUAGES_ENABLED =>
      // $this->t('Enabled installed languages (not functioning yet)'),
      // The following are from Drupal\Core\Language\LanguageInterface.
      LanguageInterface::STATE_CONFIGURABLE => $this
        ->t('All installed (enabled) languages (from /admin/config/regional/language)'),
      // const STATE_CONFIGURABLE = 1; -> 'en', 'de'
      CustomLanguageManager::LANGUAGEFIELD_LANGUAGES_CUSTOM => $this
        ->t('All custom languages (from /admin/config/regional/custom_language)'),
      LanguageInterface::STATE_LOCKED => $this
        ->t('All locked languages'),
      // const STATE_LOCKED = 2; -> 'und', 'zxx'
      // const STATE_ALL = 3; -> 'en', 'de', 'und', 'zxx'
      // LanguageInterface::STATE_ALL => $this->t('All installed languages'),
      // const STATE_SITE_DEFAULT = 4; -> 'en'
      // LanguageInterface::STATE_SITE_DEFAULT =>
      // $this->t("The site's default language"),
      // The following are copied from
      // LanguageConfiguration::getDefaultOptions()
      LanguageInterface::LANGCODE_SITE_DEFAULT => $this
        ->t("Site's default language (@language)", [
        '@language' => \Drupal::languageManager()
          ->getDefaultLanguage()
          ->getName(),
      ]),
      LanguageInterface::LANGCODE_NOT_SPECIFIED => $this
        ->t('Language neutral'),
      'current_interface' => $this
        ->t('Current interface language') . '*',
      'authors_default' => $this
        ->t("Author's preferred language") . '*',
    ],
  ];
  $element['included_languages'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Restrict by language'),
    '#default_value' => $settings['included_languages'],
    '#options' => [
      '' => $this
        ->t('- None -'),
    ] + $languages,
    '#description' => $this
      ->t('If no languages are selected, this filter will not be used.'),
    '#multiple' => TRUE,
    '#size' => 10,
  ];
  $element['excluded_languages'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Excluded languages'),
    '#default_value' => $settings['excluded_languages'],
    '#options' => [
      '' => $this
        ->t('- None -'),
    ] + $languages,
    '#description' => $this
      ->t('This removes individual languages from the list.'),
    '#multiple' => TRUE,
    '#size' => 10,
  ];
  $element['groups'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Language groups'),
    '#default_value' => $settings['groups'],
    '#description' => $this
      ->t("Provides a simple way to group common languages. If no groups are provided, no groupings will be used. Enter in the following format:<br/><code>cn,en,ep,ru<br/>African languages|bs,br<br/>Asian languages|cn,km,fil,ja</code>"),
    '#multiple' => TRUE,
    '#size' => 10,
  ];
  return $element;
}