You are here

public function LocationManager::getLocationOptionsForm in Geolocation Field 8.3

Same name and namespace in other branches
  1. 8.2 src/LocationManager.php \Drupal\geolocation\LocationManager::getLocationOptionsForm()

Get form render array.

Parameters

array $settings: Settings.

mixed $context: Optional context.

Return value

array Form.

File

src/LocationManager.php, line 74

Class

LocationManager
Search plugin manager.

Namespace

Drupal\geolocation

Code

public function getLocationOptionsForm(array $settings, $context = NULL) {
  $form = [
    '#type' => 'table',
    '#prefix' => $this
      ->t('<h3>Centre options</h3>Please note: Each option will, if it can be applied, supersede any following option.'),
    '#header' => [
      $this
        ->t('Enable'),
      $this
        ->t('Option'),
      $this
        ->t('Settings'),
      $this
        ->t('Settings'),
    ],
    '#attributes' => [
      'id' => 'geolocation-centre-options',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'geolocation-centre-option-weight',
      ],
    ],
  ];
  foreach ($this
    ->getDefinitions() as $location_id => $location_definition) {

    /** @var \Drupal\geolocation\LocationInterface $mapCenter */
    $location = $this
      ->createInstance($location_id);
    foreach ($location
      ->getAvailableLocationOptions($context) as $option_id => $label) {
      $option_enable_id = uniqid($option_id . '_enabled');
      $weight = isset($settings[$option_id]['weight']) ? $settings[$option_id]['weight'] : 0;
      $form[$option_id] = [
        '#weight' => $weight,
        '#attributes' => [
          'class' => [
            'draggable',
          ],
        ],
        'enable' => [
          '#attributes' => [
            'id' => $option_enable_id,
          ],
          '#type' => 'checkbox',
          '#default_value' => isset($settings[$option_id]['enable']) ? $settings[$option_id]['enable'] : FALSE,
        ],
        'option' => [
          '#markup' => $label,
        ],
        'weight' => [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight for @option', [
            '@option' => $label,
          ]),
          '#title_display' => 'invisible',
          '#size' => 4,
          '#default_value' => $weight,
          '#attributes' => [
            'class' => [
              'geolocation-centre-option-weight',
            ],
          ],
        ],
        'location_plugin_id' => [
          '#type' => 'value',
          '#value' => $location_id,
        ],
      ];
      $option_form = $location
        ->getSettingsForm($option_id, empty($settings[$option_id]['settings']) ? [] : $settings[$option_id]['settings'], $context);
      if (!empty($option_form)) {
        $option_form['#states'] = [
          'visible' => [
            ':input[id="' . $option_enable_id . '"]' => [
              'checked' => TRUE,
            ],
          ],
        ];
        $option_form['#type'] = 'item';
        $form[$option_id]['settings'] = $option_form;
      }
    }
  }
  uasort($form, [
    SortArray::class,
    'sortByWeightProperty',
  ]);
  return $form;
}