/** * {@inheritdoc} */ public function preBuild(array &$build, \Drupal\openlayers\Types\ObjectInterface $context = NULL) { $map_id = $context->getId(); $layers = $this->getOption('layers', array()); $items = array(); $map_layers = $context->getLayers(); $element_type = $this->getOption('multiselect', FALSE) ? 'checkbox' : 'radio'; // Only handle layers available in the map and configured in the control. // @TODO: use Form API (with form_process_* and stuff) $labels = $this->getOption('layer_labels', array()); foreach ($map_layers as $i => $map_layer) { if (isset($layers[$map_layer->machine_name])) { $classes = array(drupal_html_class($map_layer->machine_name)); $checked = ''; if ($element_type == 'checkbox') { if ($map_layer->getOption('visible', 1)) { $checked = 'checked '; $classes[] = 'active'; } } $label = check_plain($map_layer->name); if (isset($labels[$map_layer->machine_name])) { $label = openlayers_i18n_string('openlayers:layerswitcher:' . $this->machine_name . ':' . $map_layer->machine_name . ':label', $labels[$map_layer->machine_name], array('sanitize' => TRUE)); } $items[] = array('data' => '<label><input type="' . $element_type . '" name="layer" ' . $checked . 'value="' . $map_layer->machine_name . '">' . $label . '</label>', 'id' => drupal_html_id($map_id . '-' . $map_layer->machine_name), 'class' => $classes); } } $title = openlayers_i18n_string('openlayers:layerswitcher:' . $this->machine_name . ':title', $this->getOption('label', 'Layers'), array('sanitize' => TRUE)); $layerswitcher = array('#theme' => 'item_list', '#type' => 'ul', '#title' => $title, '#items' => $items, '#attributes' => array('id' => drupal_html_id($this->machine_name . '-items'))); $this->setOption('element', '<div id="' . drupal_html_id($this->machine_name) . '" class="' . drupal_html_class($this->machine_name) . ' layerswitcher">' . drupal_render($layerswitcher) . '</div>'); }
/** * {@inheritdoc} */ public function preBuild(array &$build, \Drupal\openlayers\Types\ObjectInterface $context = NULL) { $layers = $this->getOption('layers', array()); $map_layers = $context->getLayers(); // Only handle layers available in the map and configured in the control. // Ensures maximum performance on client side while having maximum // configuration flexibility. $frontend_layers = array(); foreach ($map_layers as $map_layer) { if (isset($layers[$map_layer->machine_name])) { $frontend_layers[$map_layer->machine_name] = $map_layer->machine_name; } } $this->setOption('layers', $frontend_layers); }