You are here

class LayerSwitcher in Openlayers 7.3

Class LayerSwitcher.

Plugin annotation


@OpenlayersPlugin(
 id = "LayerSwitcher",
 description = "Provides a layer switcher control."
)

Hierarchy

Expanded class hierarchy of LayerSwitcher

File

src/Plugin/Control/LayerSwitcher/LayerSwitcher.php, line 23
Control: LayerSwitcher.

Namespace

Drupal\openlayers\Plugin\Control\LayerSwitcher
View 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

Namesort descending Modifiers Type Description Overrides
Base::$attached protected property Holds all the attachment used by this object. 6
Base::$collection protected property Holds the Collection object.
Base::$id protected property A unique ID for the object.
Base::addObject public function Add an object into the collection of the parent object. Overrides ObjectInterface::addObject
Base::attached public function Returns a list of attachments for building the render array. Overrides ObjectInterface::attached 6
Base::clearOption public function Remove an option. Overrides ObjectInterface::clearOption
Base::dependencies public function Defines dependencies. Overrides ObjectInterface::dependencies 3
Base::getClassDirectory public function Returns the path to the plugin directory. Overrides ObjectInterface::getClassDirectory
Base::getClassPath public function Returns the path to the class file. Overrides ObjectInterface::getClassPath
Base::getCollection public function Return the Collection object linked to the object. Overrides ObjectInterface::getCollection
Base::getConfiguration public function Return the object configuration. Overrides ObjectInterface::getConfiguration
Base::getDependencies public function Return all the dependencies objects of the parent object. Overrides ObjectInterface::getDependencies
Base::getDescription public function Return the description of the object. Overrides ObjectInterface::getDescription
Base::getExport public function Return an object, CTools Exportable. Overrides ObjectInterface::getExport
Base::getFactoryService public function Return the Factory Service of the object. Overrides ObjectInterface::getFactoryService
Base::getId public function Return the object unique ID. Overrides ObjectInterface::getId
Base::getJS 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::getJS 6
Base::getMachineName public function Return the unique machine name of the object. Overrides ObjectInterface::getMachineName
Base::getName public function Return the human name of the object. Overrides ObjectInterface::getName
Base::getObjects public function Return an array of OL objects indexed by their type. Overrides ObjectInterface::getObjects
Base::getOption public function Returns an option. Overrides ObjectInterface::getOption
Base::getOptions public function Return the options array. Overrides ObjectInterface::getOptions
Base::getParents public function Returns an array with the maps this object is attached on. Overrides ObjectInterface::getParents
Base::getPluginDescription public function Return the description of the object's plugin. Overrides ObjectInterface::getPluginDescription
Base::getProvider public function Return the module that provides this plugin. Overrides ObjectInterface::getProvider
Base::getType public function The type of this object. Overrides ObjectInterface::getType
Base::getWeight public function Get the weight of an object. Overrides ObjectInterface::getWeight
Base::init public function Initializes the object. Overrides ObjectInterface::init 2
Base::initCollection public function Initializes the Collection, Import objects from options, Import the current object. Overrides ObjectInterface::initCollection
Base::isAsynchronous public function Whether or not this object has to be processed asynchronously. Overrides ObjectInterface::isAsynchronous 3
Base::optionsFormSubmit public function Submit callback for the options form. Overrides ObjectInterface::optionsFormSubmit 11
Base::optionsFormValidate public function Validation callback for the options form. Overrides ObjectInterface::optionsFormValidate
Base::optionsToObjects public function Return a flat array containing Openlayers Objects from the options array. Overrides ObjectInterface::optionsToObjects 9
Base::postBuild public function Invoked after an objects render array is built. Overrides ObjectInterface::postBuild 13
Base::removeObject public function Remove an object from the collection. Overrides ObjectInterface::removeObject
Base::resetCollection public function Reset the object's Collection. Overrides ObjectInterface::resetCollection
Base::setFactoryService public function Set the Factory Service of the object. Overrides ObjectInterface::setFactoryService
Base::setId public function Set the object ID. Overrides ObjectInterface::setId
Base::setOption public function Set an option. Overrides ObjectInterface::setOption
Base::setOptions public function Set the options array. Overrides ObjectInterface::setOptions
Base::setWeight public function Set the weight of an object. Overrides ObjectInterface::setWeight
Control::$options protected property The array containing the options. Overrides Base::$options
LayerSwitcher::i18nStringsRefresh public function Refresh string translations. Overrides Base::i18nStringsRefresh
LayerSwitcher::optionsForm public function @TODO What is this return? If it is the form, why is form by reference? Overrides Base::optionsForm
LayerSwitcher::preBuild public function Invoked before an objects render array is built. Overrides Base::preBuild