You are here

class LanguageConfiguration in Commerce Invoice 8.2

Defines an element for language configuration.

Plugin annotation

@FormElement("commerce_invoice_language_configuration");

Hierarchy

Expanded class hierarchy of LanguageConfiguration

1 #type use of LanguageConfiguration
InvoiceTypeForm::form in src/Form/InvoiceTypeForm.php
Gets the actual form array to be built.

File

src/Element/LanguageConfiguration.php, line 15

Namespace

Drupal\commerce_invoice\Element
View source
class LanguageConfiguration extends FormElement {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#tree' => TRUE,
      '#process' => [
        [
          $class,
          'processLanguageConfiguration',
        ],
      ],
    ];
  }

  /**
   * Process handler for the commerce_invoice_language_configuration element.
   *
   * @return array
   *   The processed element.
   */
  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;
  }

  /**
   * Returns the default options for the language configuration form element.
   *
   * @return array
   *   An array containing the default options.
   */
  protected static function getDefaultOptions() {
    $language_options = [
      LanguageInterface::LANGCODE_SITE_DEFAULT => t("Site's default language (@language)", [
        '@language' => static::languageManager()
          ->getDefaultLanguage()
          ->getName(),
      ]),
    ];
    $languages = static::languageManager()
      ->getLanguages(LanguageInterface::STATE_ALL);
    foreach ($languages as $langcode => $language) {
      if (in_array($langcode, [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        LanguageInterface::LANGCODE_NOT_APPLICABLE,
      ])) {
        continue;
      }
      $language_options[$langcode] = $language
        ->isLocked() ? t('- @name -', [
        '@name' => $language
          ->getName(),
      ]) : $language
        ->getName();
    }
    return $language_options;
  }

  /**
   * Wraps the language manager.
   *
   * @return \Drupal\Core\Language\LanguageManagerInterface
   *   The language manager.
   */
  protected static function languageManager() {
    return \Drupal::languageManager();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormElement::processAutocomplete public static function Adds autocomplete functionality to elements.
FormElement::processPattern public static function #process callback for #pattern form element property.
FormElement::validatePattern public static function #element_validate callback for #pattern form element property.
FormElement::valueCallback public static function Determines how user input is mapped to an element's #value property. Overrides FormElementInterface::valueCallback 15
LanguageConfiguration::getDefaultOptions protected static function Returns the default options for the language configuration form element.
LanguageConfiguration::getInfo public function Returns the element properties for this element. Overrides ElementInterface::getInfo
LanguageConfiguration::languageManager protected static function Wraps the language manager.
LanguageConfiguration::processLanguageConfiguration public static function Process handler for the commerce_invoice_language_configuration element.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
RenderElement::preRenderAjaxForm public static function Adds Ajax information about an element to communicate with JavaScript.
RenderElement::preRenderGroup public static function Adds members of this group as actual elements for rendering.
RenderElement::processAjaxForm public static function Form element processing handler for the #ajax form property. 1
RenderElement::processGroup public static function Arranges elements into groups.
RenderElement::setAttributes public static function Sets a form element's class attribute. Overrides ElementInterface::setAttributes
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.