You are here

function openlayers_map_form_layers in Openlayers 7.3

Map layers config form handler.

1 string reference to 'openlayers_map_form_layers'
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 153
CTools Export UI plugin definition for maps.

Code

function openlayers_map_form_layers($form, &$form_state) {
  if (($map = \Drupal\openlayers\Openlayers::load('Map', $form_state['item'])) == FALSE) {
    return;
  }
  $all_layers = \Drupal\openlayers\Openlayers::loadAll('Layer');
  array_walk($all_layers, function (\Drupal\openlayers\Types\LayerInterface $layer) {
    $layer
      ->setWeight(0);
    $layer->enabled = 0;
  });
  foreach ($map
    ->getOption('layers', array()) as $weight => $layer) {

    /** @var Drupal\openlayers\Types\LayerInterface $layer */
    $layer = \Drupal\openlayers\Openlayers::load('layer', $layer);
    if (isset($all_layers[$layer
      ->getMachineName()])) {
      $all_layers[$layer
        ->getMachineName()]
        ->setWeight($weight);
      $all_layers[$layer
        ->getMachineName()]->enabled = 1;
    }
  }
  uasort($all_layers, function (\Drupal\openlayers\Types\LayerInterface $a, \Drupal\openlayers\Types\LayerInterface $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;

  /** @var \Drupal\openlayers\Types\Layer $layer */
  foreach ($all_layers as $machine_name => $layer) {
    $operations = array();
    $operations[] = l(t('Edit layer'), 'admin/structure/openlayers/layers/list/' . $layer
      ->getMachineName() . '/edit/options', array(
      'query' => array(
        'destination' => current_path(),
      ),
    ));
    if ($source = $layer
      ->getSource()) {
      $operations[] = l(t('Edit source'), 'admin/structure/openlayers/sources/list/' . $source
        ->getMachineName() . '/edit/options', array(
        'query' => array(
          'destination' => current_path(),
        ),
      ));
      $source = (array) $source
        ->getExport();
      $source_properties = array(
        t('@name (<em>@machine_name</em>)', array(
          '@name' => $source['name'],
          '@machine_name' => $source['machine_name'],
        )),
        t('Factory service: <em>@factory_service</em>', array(
          '@factory_service' => $source['factory_service'],
        )),
      );
    }
    else {
      $source = array();
      $source_properties = array(
        t('Undefined'),
      );
    }
    $source += array(
      'name' => t('Undefined'),
      'machine_name' => t('Undefined'),
      'factory_service' => t('Undefined'),
    );
    $operations = implode('<br/>', $operations);
    $layer_properties = array(
      t('@name (<em>@machine_name</em>)', array(
        '@name' => $layer
          ->getName(),
        '@machine_name' => $layer
          ->getMachineName(),
      )),
      t('Factory service: <em>@factory_service</em>', array(
        '@factory_service' => $layer
          ->getFactoryService(),
      )),
    );
    $data[$machine_name] = array(
      'layer_properties' => implode('<br/>', $layer_properties),
      'source_properties' => implode('<br/>', $source_properties),
      'machine_name' => $layer
        ->getMachineName(),
      'factory_service' => $layer
        ->getFactoryService(),
      'operations' => $operations,
      'weight' => $i++,
      'enabled' => isset($layer->enabled) ? $layer->enabled : 0,
      'default' => 1,
      'style' => $layer
        ->getOption('style', NULL),
    );
  }
  $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(
              'layers',
              $id,
              'weight',
            ),
            '#attributes' => array(
              'class' => array(
                'entry-order-weight',
              ),
            ),
          ),
        ),
        array(
          'data' => array(
            '#type' => 'hidden',
            '#default_value' => $entry['machine_name'],
            '#parents' => array(
              'layers',
              $id,
              'machine_name',
            ),
          ),
        ),
        array(
          'data' => array(
            '#type' => 'checkbox',
            '#title' => t('Enable'),
            '#title_display' => 'invisible',
            '#default_value' => $entry['enabled'],
            '#parents' => array(
              'layers',
              $id,
              'enabled',
            ),
          ),
        ),
        array(
          'data' => array(
            '#type' => 'select',
            '#title' => t('Style'),
            '#title_display' => 'invisible',
            '#options' => array(
              '' => t('- Select a Style -'),
            ) + \Drupal\openlayers\Openlayers::loadAllAsOptions('style'),
            '#default_value' => $entry['style'],
            '#parents' => array(
              'layers',
              $id,
              'style',
            ),
          ),
        ),
        $entry['layer_properties'],
        $entry['source_properties'],
        $entry['operations'],
      ),
      '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'],
      'style' => &$rows[$id]['data'][3]['data'],
      'machine_name' => &$rows[$id]['data'][4]['data'],
    );
  }

  // Add the table to the form.
  $form['layers']['table_layers'] = 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,
      ),
      array(
        'data' => t('Enabled'),
        'colspan' => 2,
      ),
      array(
        'data' => t('Layer style'),
        'colspan' => 1,
      ),
      t('Layer properties'),
      t('Source properties'),
      t('Operations'),
    ),
    '#rows' => $rows,
    '#empty' => t('There are no entries available.'),
    '#attributes' => array(
      'id' => 'entry-order-layers',
    ),
  );
  drupal_add_tabledrag('entry-order-layers', 'order', 'sibling', 'entry-order-weight');
  return $form;
}