class LayerSwitcher in Openlayers 7.3
Class LayerSwitcher.
Plugin annotation
@OpenlayersPlugin(
id = "LayerSwitcher",
description = "Provides a layer switcher control."
)
Hierarchy
- class \Drupal\openlayers\Types\Base extends \Drupal\Component\Plugin\PluginBase implements ObjectInterface
- class \Drupal\openlayers\Types\Control implements ControlInterface
- class \Drupal\openlayers\Plugin\Control\LayerSwitcher\LayerSwitcher
- class \Drupal\openlayers\Types\Control implements ControlInterface
Expanded class hierarchy of LayerSwitcher
File
- src/
Plugin/ Control/ LayerSwitcher/ LayerSwitcher.php, line 23 - Control: LayerSwitcher.
Namespace
Drupal\openlayers\Plugin\Control\LayerSwitcherView source
class LayerSwitcher extends Control {
/**
* {@inheritdoc}
*/
public function optionsForm(array &$form, array &$form_state) {
$form['options']['label'] = array(
'#type' => 'textfield',
'#title' => t('Title of the control'),
'#default_value' => $this
->getOption('label', 'Layers'),
);
$form['options']['layers'] = array(
'#type' => 'select',
'#title' => t('Layers'),
'#empty_option' => t('- Select a Layer -'),
'#multiple' => TRUE,
'#default_value' => $this
->getOption('layers'),
'#options' => Openlayers::loadAllAsOptions('Layer'),
);
$form['options']['multiselect'] = array(
'#type' => 'checkbox',
'#title' => t('Allow selecting multiple layers'),
'#default_value' => $this
->getOption('multiselect', FALSE),
);
$form['options']['layer_labels_hint'] = array(
'#markup' => t('You need to save the configuration before being able to set custom layer labels.'),
);
$labels = $this
->getOption('layer_labels', array());
foreach ((array) $this
->getOption('layers') as $i => $machine_name) {
if (($map_layer = Openlayers::load('Layer', $machine_name)) == TRUE) {
$label = check_plain($map_layer
->getName());
if (isset($labels[$machine_name])) {
$label = $labels[$machine_name];
}
$form['options']['layer_labels'][$machine_name] = array(
'#type' => 'textfield',
'#title' => t('Label for layer @label:', array(
'@label' => $map_layer
->getName(),
)),
'#default_value' => $label,
);
}
}
// @TODO Add configuration for initial visibility. (Adjust JS accordingly)
// @TODO Add configuration for ordering?
}
/**
* {@inheritdoc}
*/
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);
}
/**
* {@inheritdoc}
*/
public function i18nStringsRefresh() {
// Ensure just labels of active layers are stored.
$labels = $this
->getOption('layer_labels', array());
$layers = $this
->getOption('layers', array());
$existing_layers = array_intersect_key($labels, $layers);
$removed_layers = array_diff_key($layers, $labels);
// Handle translatable values.
// Remove / register string translations.
foreach ($removed_layers as $layer) {
openlayers_i18n_string_remove('openlayers:layerswitcher:' . $this
->getMachineName() . ':' . $layer . ':label');
}
foreach ($existing_layers as $layer => $label) {
openlayers_i18n_string_update('openlayers:layerswitcher:' . $this
->getMachineName() . ':' . $layer . ':label', $label);
}
// Register string in i18n string if possible.
openlayers_i18n_string_update('openlayers:layerswitcher:' . $this
->getMachineName() . ':title', $this
->getOption('label', 'Layers'));
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Base:: |
protected | property | Holds all the attachment used by this object. | 6 |
Base:: |
protected | property | Holds the Collection object. | |
Base:: |
protected | property | A unique ID for the object. | |
Base:: |
public | function |
Add an object into the collection of the parent object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Returns a list of attachments for building the render array. Overrides ObjectInterface:: |
6 |
Base:: |
public | function |
Remove an option. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Defines dependencies. Overrides ObjectInterface:: |
3 |
Base:: |
public | function |
Returns the path to the plugin directory. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Returns the path to the class file. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the Collection object linked to the object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the object configuration. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return all the dependencies objects of the parent object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the description of the object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return an object, CTools Exportable. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the Factory Service of the object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the object unique ID. Overrides ObjectInterface:: |
|
Base:: |
public | function |
!Attention! This function will remove any option that is named after a
plugin type e.g.: layers, controls, styles, interactions, components . Overrides ObjectInterface:: |
6 |
Base:: |
public | function |
Return the unique machine name of the object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the human name of the object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return an array of OL objects indexed by their type. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Returns an option. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the options array. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Returns an array with the maps this object is attached on. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the description of the object's plugin. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return the module that provides this plugin. Overrides ObjectInterface:: |
|
Base:: |
public | function |
The type of this object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Get the weight of an object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Initializes the object. Overrides ObjectInterface:: |
2 |
Base:: |
public | function |
Initializes the Collection,
Import objects from options,
Import the current object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Whether or not this object has to be processed asynchronously. Overrides ObjectInterface:: |
3 |
Base:: |
public | function |
Submit callback for the options form. Overrides ObjectInterface:: |
11 |
Base:: |
public | function |
Validation callback for the options form. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Return a flat array containing Openlayers Objects from the options array. Overrides ObjectInterface:: |
9 |
Base:: |
public | function |
Invoked after an objects render array is built. Overrides ObjectInterface:: |
13 |
Base:: |
public | function |
Remove an object from the collection. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Reset the object's Collection. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Set the Factory Service of the object. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Set the object ID. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Set an option. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Set the options array. Overrides ObjectInterface:: |
|
Base:: |
public | function |
Set the weight of an object. Overrides ObjectInterface:: |
|
Control:: |
protected | property |
The array containing the options. Overrides Base:: |
|
LayerSwitcher:: |
public | function |
Refresh string translations. Overrides Base:: |
|
LayerSwitcher:: |
public | function |
@TODO What is this return? If it is the form, why is form by reference? Overrides Base:: |
|
LayerSwitcher:: |
public | function |
Invoked before an objects render array is built. Overrides Base:: |