You are here

function openlayers_map_form_components in Openlayers 7.3

Map components config form handler.

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

Code

function openlayers_map_form_components($form, &$form_state) {
  $map = \Drupal\openlayers\Openlayers::load('map', $form_state['item']);
  $all_components = \Drupal\openlayers\Openlayers::loadAll('Component');
  array_walk($all_components, function (\Drupal\openlayers\Types\ComponentInterface $component) {
    $component
      ->setWeight(0);
    $component->enabled = 0;
  });
  foreach ($map
    ->getObjects('component') as $component) {

    /** @var Drupal\openlayers\Types\Component $component */
    $all_components[$component
      ->getMachineName()]
      ->setWeight($component
      ->getWeight());
    $all_components[$component
      ->getMachineName()]->enabled = 1;
  }
  uasort($all_components, function (\Drupal\openlayers\Types\ComponentInterface $a, \Drupal\openlayers\Types\ComponentInterface $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_components as $component) {
    $data[$component
      ->getMachineName()] = array(
      'name' => $component
        ->getName(),
      'machine_name' => $component
        ->getMachineName(),
      'description' => $component
        ->getDescription(),
      'weight' => $i++,
      'enabled' => $component->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(
              'components',
              $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(
              'components',
              $id,
              'enabled',
            ),
          ),
        ),
        check_plain($entry['name']),
        check_plain($entry['machine_name']),
        check_plain($entry['description']),
        l(t('Edit'), 'admin/structure/openlayers/components/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['components']['table_components'] = 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 weight 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-components',
    ),
  );
  drupal_add_tabledrag('entry-order-components', 'order', 'sibling', 'entry-order-weight');
  return $form;
}