You are here

public function MapCenterManager::getCenterOptionsForm in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/MapCenterManager.php \Drupal\geolocation\MapCenterManager::getCenterOptionsForm()

Get form render array.

Parameters

array $settings: Settings.

mixed $context: Optional context.

Return value

array Form.

File

src/MapCenterManager.php, line 72

Class

MapCenterManager
Search plugin manager.

Namespace

Drupal\geolocation

Code

public function getCenterOptionsForm(array $settings, $context = NULL) {
  $form = [
    '#type' => 'table',
    '#prefix' => t('<h3>Centre override</h3>These options allow to override the default map centre. Each option will, if it can be applied, supersede any following option.'),
    '#header' => [
      [
        'data' => t('Enable'),
        'colspan' => 2,
      ],
      t('Option'),
      t('Settings'),
      t('Weight'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'geolocation-centre-option-weight',
      ],
    ],
  ];
  foreach ($this
    ->getDefinitions() as $map_center_id => $map_center_definition) {

    /** @var \Drupal\geolocation\MapCenterInterface $map_center */
    $map_center = $this
      ->createInstance($map_center_id);
    foreach ($map_center
      ->getAvailableMapCenterOptions($context) as $option_id => $label) {
      $option_enable_id = HTML::getUniqueId($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,
        ],
        'map_center_id' => [
          '#type' => 'value',
          '#value' => $map_center_id,
        ],
        'option' => [
          '#markup' => $label,
        ],
        'settings' => [
          '#markup' => '',
        ],
        'weight' => [
          '#type' => 'weight',
          '#title' => t('Weight for @option', [
            '@option' => $label,
          ]),
          '#title_display' => 'invisible',
          '#size' => 4,
          '#default_value' => $weight,
          '#attributes' => [
            'class' => [
              'geolocation-centre-option-weight',
            ],
          ],
        ],
      ];
      $map_center_settings = [];
      if (!empty($settings[$option_id]['settings'])) {
        $map_center_settings = $settings[$option_id]['settings'];
      }
      $option_form = $map_center
        ->getSettingsForm($option_id, $map_center
        ->getSettings($map_center_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;
}