You are here

protected function DomainLangNegotiationConfigureForm::configureFormTable in Domain Lang 8

Builds a language negotiation method configuration table.

Parameters

array $form: The language negotiation configuration form.

string $type: The language type to generate the table for.

Overrides NegotiationConfigureForm::configureFormTable

1 call to DomainLangNegotiationConfigureForm::configureFormTable()
DomainLangNegotiationConfigureForm::buildForm in src/Form/DomainLangNegotiationConfigureForm.php
Form constructor.

File

src/Form/DomainLangNegotiationConfigureForm.php, line 194

Class

DomainLangNegotiationConfigureForm
Configure the selected language negotiation method for this site.

Namespace

Drupal\domain_lang\Form

Code

protected function configureFormTable(array &$form, $type) {
  $info = $form['#language_types_info'][$type];
  $table_form = [
    '#title' => $this
      ->t('@type language detection', [
      '@type' => $info['name'],
    ]),
    '#tree' => TRUE,
    '#description' => $info['description'],
    '#language_negotiation_info' => [],
    '#show_operations' => FALSE,
    'weight' => [
      '#tree' => TRUE,
    ],
  ];

  // Only show configurability checkbox for the unlocked language types.
  if (empty($info['locked'])) {
    $configurable = $this->languageTypes
      ->get('configurable');
    $table_form['configurable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Customize %language_name language detection to differ from Interface text language detection settings', [
        '%language_name' => $info['name'],
      ]),
      '#default_value' => in_array($type, $configurable),
      '#attributes' => [
        'class' => [
          'language-customization-checkbox',
        ],
      ],
      '#attached' => [
        'library' => [
          'language/drupal.language.admin',
        ],
      ],
    ];
  }
  $negotiation_info = $form['#language_negotiation_info'];
  $enabled_methods = $this->languageTypes
    ->get('negotiation.' . $type . '.enabled') ?: [];
  $methods_weight = $this->languageTypes
    ->get('negotiation.' . $type . '.method_weights') ?: [];

  // Add missing data to the methods lists.
  foreach ($negotiation_info as $method_id => $method) {
    if (!isset($methods_weight[$method_id])) {
      $methods_weight[$method_id] = isset($method['weight']) ? $method['weight'] : 0;
    }
  }

  // Order methods list by weight.
  asort($methods_weight);
  foreach ($methods_weight as $method_id => $weight) {

    // A language method might be no more available if the defining module has
    // been disabled after the last configuration saving.
    if (!isset($negotiation_info[$method_id])) {
      continue;
    }
    $enabled = isset($enabled_methods[$method_id]);
    $method = $negotiation_info[$method_id];

    // List the method only if the current type is defined in its 'types' key.
    // If it is not defined default to all the configurable language types.
    $types = array_flip(isset($method['types']) ? $method['types'] : $form['#language_types']);
    if (isset($types[$type])) {
      $table_form['#language_negotiation_info'][$method_id] = $method;
      $method_name = $method['name'];
      $table_form['weight'][$method_id] = [
        '#type' => 'weight',
        '#title' => $this
          ->t('Weight for @title language detection method', [
          '@title' => mb_strtolower($method_name),
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $weight,
        '#attributes' => [
          'class' => [
            "language-method-weight-{$type}",
          ],
        ],
        '#delta' => 20,
      ];
      $table_form['title'][$method_id] = [
        '#plain_text' => $method_name,
      ];
      $table_form['enabled'][$method_id] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable @title language detection method', [
          '@title' => mb_strtolower($method_name),
        ]),
        '#title_display' => 'invisible',
        '#default_value' => $enabled,
      ];
      if ($method_id === LanguageNegotiationSelected::METHOD_ID) {
        $table_form['enabled'][$method_id]['#default_value'] = TRUE;
        $table_form['enabled'][$method_id]['#attributes'] = [
          'disabled' => 'disabled',
        ];
      }
      $table_form['description'][$method_id] = [
        '#markup' => $method['description'],
      ];
      $config_op = [];
      if (isset($method['config_route_name'])) {
        $new_route = 'domain_lang.' . substr($method['config_route_name'], 9);
        try {
          $this->routeProvider
            ->getRouteByName($new_route);
          $config_url = Url::fromRoute($new_route, [
            'domain' => $this->domainLangHandler
              ->getDomainFromUrl()
              ->id(),
          ]);
        } catch (RouteNotFoundException $e) {
          $config_url = Url::fromRoute($method['config_route_name']);
        }
        $config_op['configure'] = [
          'title' => $this
            ->t('Configure'),
          'url' => $config_url,
        ];

        // If there is at least one operation enabled show the operation
        // column.
        $table_form['#show_operations'] = TRUE;
      }
      $table_form['operation'][$method_id] = [
        '#type' => 'operations',
        '#links' => $config_op,
      ];
    }
  }
  $form[$type] = $table_form;
}