public function LayerSwitcher::preBuild in Openlayers 7.3
Invoked before an objects render array is built.
Mostly invoked by the map object.
Parameters
array $build: The array with the build information.
\Drupal\openlayers\Types\ObjectInterface $context: The context of the build. Mostly the map object.
Overrides Base::preBuild
File
- src/
Plugin/ Control/ LayerSwitcher/ LayerSwitcher.php, line 73 - Control: LayerSwitcher.
Class
- LayerSwitcher
- Class LayerSwitcher.
Namespace
Drupal\openlayers\Plugin\Control\LayerSwitcherCode
public function preBuild(array &$build, ObjectInterface $context = NULL) {
$map_id = $context
->getId();
$layers = $this
->getOption('layers', array());
$items = array();
$map_layers = $context
->getObjects('layer');
$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
->getMachineName()])) {
$classes = array(
drupal_html_class($map_layer
->getMachineName()),
);
$checked = '';
if ($element_type == 'checkbox') {
if ($map_layer
->getOption('visible', 1)) {
$checked = 'checked ';
$classes[] = 'active';
}
}
$label = $map_layer
->getName();
if (isset($labels[$map_layer
->getMachineName()])) {
$label = openlayers_i18n_string('openlayers:layerswitcher:' . $this
->getMachineName() . ':' . $map_layer
->getMachineName() . ':label', $labels[$map_layer
->getMachineName()], array(
'sanitize' => TRUE,
));
}
$items[] = array(
'data' => '<label><input type="' . $element_type . '" name="layer" ' . $checked . 'value="' . $map_layer
->getMachineName() . '">' . $label . '</label>',
'id' => drupal_html_id($map_id . '-' . $map_layer
->getMachineName()),
'class' => $classes,
);
}
}
$title = openlayers_i18n_string('openlayers:layerswitcher:' . $this
->getMachineName() . ':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
->getMachineName() . '-items'),
),
);
$this
->setOption('element', '<div id="' . drupal_html_id($this
->getMachineName()) . '" class="' . drupal_html_class($this
->getMachineName()) . ' layerswitcher">' . drupal_render($layerswitcher) . '</div>');
// Allow the parent class to perform it's pre-build actions.
parent::preBuild($build, $context);
}