You are here

function openlayers_map_form_controls in Openlayers 7.3

Map controls config form handler.

1 string reference to 'openlayers_map_form_controls'
openlayers_ui_OpenlayersMaps_ctools_export_ui in modules/openlayers_ui/src/Plugin/export_ui/OpenlayersMaps.inc
CTools Export UI plugin definition.

File

modules/openlayers_ui/src/Plugin/export_ui/OpenlayersMaps.inc, line 376
CTools Export UI plugin definition for maps.

Code

function openlayers_map_form_controls($form, &$form_state) {
  $map = \Drupal\openlayers\Openlayers::load('map', $form_state['item']);
  $all_controls = \Drupal\openlayers\Openlayers::loadAll('Control');
  if (!isset($form_state['item']->options['controls'])) {
    $form_state['item']->options['controls'] = array();
  }
  array_walk($all_controls, function (\Drupal\openlayers\Types\ControlInterface $control) {
    $control
      ->setWeight(0);
    $control->enabled = 0;
  });
  foreach ($map
    ->getObjects('control') as $control) {

    /** @var Drupal\openlayers\Types\Control $control */
    $all_controls[$control
      ->getMachineName()]
      ->setWeight($control
      ->getWeight());
    $all_controls[$control
      ->getMachineName()]->enabled = 1;
  }
  uasort($all_controls, function (\Drupal\openlayers\Types\ControlInterface $a, \Drupal\openlayers\Types\ControlInterface $b) {
    if ($a->enabled > $b->enabled) {
      return -1;
    }
    elseif ($a->enabled < $b->enabled) {
      return 1;
    }
    if ($a
      ->getWeight() < $b
      ->getWeight()) {
      return -1;
    }
    elseif ($a
      ->getWeight() > $b
      ->getWeight()) {
      return 1;
    }
    if ($a
      ->getMachineName() < $b
      ->getMachineName()) {
      return -1;
    }
    elseif ($a
      ->getMachineName() > $b
      ->getMachineName()) {
      return 1;
    }
    return 0;
  });
  $data = array();
  $i = 0;
  foreach ($all_controls as $machine_name => $control) {
    $data[$machine_name] = array(
      'name' => $control
        ->getName(),
      'machine_name' => $control
        ->getMachineName(),
      'description' => $control
        ->getDescription(),
      'weight' => $i++,
      'enabled' => (int) $control->enabled,
    );
  }
  $rows = array();
  $row_elements = array();
  foreach ($data as $id => $entry) {
    $rows[$id] = array(
      'data' => array(
        array(
          'class',
          array(
            'entry-cross',
          ),
        ),
        array(
          'data' => array(
            '#type' => 'weight',
            '#title' => t('Weight'),
            '#title_display' => 'invisible',
            '#default_value' => $entry['weight'],
            '#parents' => array(
              'controls',
              $id,
              'weight',
            ),
            '#attributes' => array(
              'class' => array(
                'entry-order-weight',
              ),
            ),
          ),
        ),
        array(
          'data' => array(
            '#type' => 'checkbox',
            '#title' => t('Enable'),
            '#title_display' => 'invisible',
            '#default_value' => $entry['enabled'],
            '#parents' => array(
              'controls',
              $id,
              'enabled',
            ),
          ),
        ),
        check_plain($entry['name']),
        check_plain($entry['machine_name']),
        check_plain($entry['description']),
        l(t('Edit'), 'admin/structure/openlayers/controls/list/' . $entry['machine_name'] . '/edit', array(
          'query' => array(
            'destination' => current_path(),
          ),
        )),
      ),
      'class' => array(
        'draggable',
      ),
    );

    // Build rows of the form elements in the table.
    $row_elements[$id] = array(
      'weight' => &$rows[$id]['data'][1]['data'],
      'enabled' => &$rows[$id]['data'][2]['data'],
    );
  }
  $form['options']['#tree'] = TRUE;

  // Add the table to the form.
  $form['controls']['table_controls'] = array(
    '#theme' => 'table',
    // The row form elements need to be processed and build,
    // therefore pass them as element children.
    'elements' => $row_elements,
    '#header' => array(
      // We need two empty columns for the weigth field and the cross.
      array(
        'data' => NULL,
        'colspan' => 2,
      ),
      t('Enabled'),
      t('Name'),
      t('Machine name'),
      t('Description'),
      t('Operations'),
    ),
    '#rows' => $rows,
    '#empty' => t('There are no entries available.'),
    '#attributes' => array(
      'id' => 'entry-order-controls',
    ),
  );
  drupal_add_tabledrag('entry-order-controls', 'order', 'sibling', 'entry-order-weight');
  return $form;
}