/** * Load all objects. * * @param string $object_type * Type of object to load: * map|layer|source|control|interaction|style|component. * * @return \Drupal\openlayers\Types\ObjectInterface[] * The array of objects. */ public static function loadAll($object_type = NULL) { $objects = array(); foreach (Openlayers::loadAllExportable($object_type) as $exportable) { if (is_object($exportable)) { $objects[$exportable->machine_name] = Openlayers::load($object_type, $exportable); } } return $objects; }
/** * {@inheritdoc} */ public function optionsForm(&$form, &$form_state) { $form['options']['grouptitle'] = array('#type' => 'textfield', '#title' => 'Layer group title', '#default_value' => $this->getOption('grouptitle', 'Base layers')); $all_layers = \Drupal\openlayers\Openlayers::loadAllExportable('Layer'); array_walk($all_layers, function ($object) { $object->weight = 0; $object->enabled = 0; }); foreach ($this->getOption('grouplayers', array()) as $weight => $layer) { $layer = Openlayers::load('layer', $layer); $all_layers[$layer->getMachineName()]->weight = $weight; $all_layers[$layer->getMachineName()]->enabled = 1; } uasort($all_layers, function ($a, $b) { if ($a->enabled > $b->enabled) { return -1; } else { if ($a->enabled < $b->enabled) { return 1; } } if ($a->weight < $b->weight) { return -1; } else { if ($a->weight > $b->weight) { return 1; } } if ($a->machine_name < $b->machine_name) { return -1; } else { if ($a->machine_name > $b->machine_name) { return 1; } } return 0; }); $data = array(); $i = 0; /* @var \Drupal\openlayers\Types\Layer $layer */ foreach ($all_layers as $machine_name => $layer) { $data[$machine_name] = array('name' => $layer->name, 'machine_name' => $layer->machine_name, 'factory_service' => $layer->factory_service, 'weight' => $i++, 'enabled' => isset($layer->enabled) ? $layer->enabled : 0); } $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('grouplayers', $id, 'weight'), '#attributes' => array('class' => array('entry-order-weight')))), array('data' => array('#type' => 'hidden', '#default_value' => $entry['machine_name'], '#parents' => array('grouplayers', $id, 'machine_name'))), array('data' => array('#type' => 'checkbox', '#title' => t('Enable'), '#title_display' => 'invisible', '#default_value' => $entry['enabled'], '#parents' => array('grouplayers', $id, 'enabled'))), check_plain($entry['name']), check_plain($entry['machine_name']), check_plain($entry['factory_service']), l(t('Edit'), 'admin/structure/openlayers/layers/list/' . $entry['machine_name'] . '/edit/options', 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'], 'machine_name' => &$rows[$id]['data'][3]['data']); } $form['grouplayers'] = array('#type' => 'fieldset', '#title' => 'Layers', '#collapsible' => FALSE, '#collapsed' => FALSE); // Add the table to the form. $form['grouplayers']['table_layers'] = array('#theme' => 'table', 'elements' => $row_elements, '#header' => array(array('data' => NULL, 'colspan' => 2), array('data' => t('Enabled'), 'colspan' => 2), t('Name'), t('Machine name'), t('Factory service'), 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'); }
/** * {@inheritdoc} */ public function postBuild(array &$build, ObjectInterface $context = NULL) { parent::postBuild($build, $context); $options = array(); foreach (\Drupal\openlayers\Openlayers::loadAllExportable('Map') as $machine_name => $data) { if (!is_object($data) || property_exists($data, 'disabled') && ($data->disabled == 1 || $data->disabled == TRUE)) { continue; } $options[$machine_name] = $data->name; } $wrapper = 'wrapper-' . $context->getId(); $build['openlayers_default_map'] = array('#type' => 'select', '#title' => 'Chose a map', '#multiple' => FALSE, '#options' => $options, '#ajax' => array('callback' => '_openlayers_ajax_reload_default_map', 'method' => 'replace', 'wrapper' => $wrapper, 'effect' => 'fade')); $build['form'] = array('#type' => 'container', '#attributes' => array('id' => $wrapper)); $build['form'][$context->getId()]['map'] = $build['openlayers']; unset($build['openlayers']); }