You are here

public static function LanguageConfiguration::processLanguageConfiguration in Commerce Invoice 8.2

Process handler for the commerce_invoice_language_configuration element.

Return value

array The processed element.

File

src/Element/LanguageConfiguration.php, line 37

Class

LanguageConfiguration
Defines an element for language configuration.

Namespace

Drupal\commerce_invoice\Element

Code

public static function processLanguageConfiguration(&$element, FormStateInterface $form_state, &$form) {
  $options = isset($element['#options']) ? $element['#options'] : [];

  // Avoid validation failure since we are moving the '#options' key in the
  // nested 'language' select element.
  unset($element['#options']);

  /** @var \Drupal\language\Entity\ContentLanguageSettings $default_config */
  $default_config = $element['#default_value'];
  $element['langcode'] = [
    '#type' => 'select',
    '#title' => t('Default language'),
    '#options' => $options + static::getDefaultOptions(),
    '#description' => t('Explanation of the language options is found on the <a href=":languages_list_page">languages list page</a>.', [
      ':languages_list_page' => Url::fromRoute('entity.configurable_language.collection')
        ->toString(),
    ]),
    '#default_value' => $default_config != NULL ? $default_config
      ->getDefaultLangcode() : LanguageInterface::LANGCODE_SITE_DEFAULT,
  ];
  $element['generate_translations'] = [
    '#type' => 'checkbox',
    '#title' => t('Generate translations for each of the available languages (on invoice generation)'),
    '#default_value' => $default_config != NULL ? $default_config
      ->getThirdPartySetting('commerce_invoice', 'generate_translations', FALSE) : FALSE,
  ];

  // Add the entity type and bundle information to the form if they are set.
  // They will be used, in the submit handler, to generate the names of the
  // configuration entities that will store the settings and are a way to uniquely
  // identify the entity.
  $language = $form_state
    ->get('language') ?: [];
  $language += [
    $element['#name'] => [
      'entity_type' => $element['#entity_information']['entity_type'],
      'bundle' => $element['#entity_information']['bundle'],
    ],
  ];
  $form_state
    ->set('language', $language);
  $form['actions']['submit']['#submit'][] = 'commerce_invoice_language_configuration_element_submit';
  return $element;
}