You are here

class OLMap in Openlayers 7.3

Class OLMap.

Plugin annotation


@OpenlayersPlugin(
 id = "OLMap"
)

Hierarchy

  • class \Drupal\openlayers\Types\Base extends \Drupal\Component\Plugin\PluginBase implements ObjectInterface
    • class \Drupal\openlayers\Types\Map implements MapInterface
      • class \Drupal\openlayers\Plugin\Map\OLMap\OLMap

Expanded class hierarchy of OLMap

File

src/Plugin/Map/OLMap/OLMap.php, line 20
Map: Map.

Namespace

Drupal\openlayers\Plugin\Map\OLMap
View source
class OLMap extends Map {

  /**
   * {@inheritdoc}
   */
  public function optionsForm(array &$form, array &$form_state) {
    $form['options']['ui'] = array(
      '#type' => 'fieldset',
      '#title' => t('User interface'),
      'width' => array(
        '#type' => 'textfield',
        '#title' => 'Width of the map',
        '#default_value' => $this
          ->getOption('width', 'auto'),
        '#parents' => array(
          'options',
          'width',
        ),
      ),
      'height' => array(
        '#type' => 'textfield',
        '#title' => t('Height of the map'),
        '#default_value' => $this
          ->getOption('height', '300px'),
        '#parents' => array(
          'options',
          'height',
        ),
      ),
    );
    $form['options']['view'] = array(
      '#type' => 'fieldset',
      '#title' => t('Center and rotation'),
      '#tree' => TRUE,
    );
    if ($this
      ->getMachineName() != Config::get('openlayers.edit_view_map')) {
      if (($map = Openlayers::load('Map', Config::get('openlayers.edit_view_map'))) == TRUE) {
        $map_configuration = $map
          ->getConfiguration();
        if ($view = $this
          ->getOption('view')) {

          // Don't apply min / max zoom settings to this map to avoid lock-in.
          $view['minZoom'] = 0;
          $view['maxZoom'] = 0;
          $map
            ->setOption('view', $view);
        }
        $form['options']['view']['map'] = array(
          '#type' => 'openlayers',
          '#description' => $map
            ->getPluginDescription(),
          '#map' => $map,
        );
      }
    }
    $form['options']['view']['center'] = array(
      '#tree' => TRUE,
      'lon' => array(
        '#type' => 'textfield',
        '#title' => t('Longitude (X axis)'),
        '#default_value' => $this
          ->getOption(array(
          'view',
          'center',
          'lon',
        ), 0),
      ),
      'lat' => array(
        '#type' => 'textfield',
        '#title' => t('Latitude (Y axis)'),
        '#default_value' => $this
          ->getOption(array(
          'view',
          'center',
          'lat',
        ), 0),
      ),
    );
    $form['options']['view']['rotation'] = array(
      '#type' => 'textfield',
      '#title' => t('Rotation'),
      '#default_value' => $this
        ->getOption(array(
        'view',
        'rotation',
      ), 0),
    );
    $form['options']['view']['zoom'] = array(
      '#type' => 'textfield',
      '#title' => t('Zoom'),
      '#default_value' => $this
        ->getOption(array(
        'view',
        'zoom',
      ), 0),
    );
    $form['options']['view']['minZoom'] = array(
      '#type' => 'textfield',
      '#title' => t('Min zoom'),
      '#default_value' => $this
        ->getOption(array(
        'view',
        'minZoom',
      ), 0),
    );
    $form['options']['view']['maxZoom'] = array(
      '#type' => 'textfield',
      '#title' => t('Max zoom'),
      '#default_value' => $this
        ->getOption(array(
        'view',
        'maxZoom',
      ), 0),
    );
    $form['options']['view']['limit_extent'] = array(
      '#type' => 'select',
      '#title' => t('Limit to extent'),
      '#empty_option' => t('Disabled'),
      '#empty_value' => '',
      '#options' => array(
        'custom' => 'Custom extent',
        'projection' => 'Map projection',
      ),
      '#description' => t('If enabled navigation on the map is limited to the give extent.'),
      '#default_value' => $this
        ->getOption(array(
        'view',
        'limit_extent',
      ), FALSE),
    );
    $form['options']['view']['extent'] = array(
      '#type' => 'textfield',
      '#title' => t('Extent [minx, miny, maxx, maxy]'),
      '#default_value' => $this
        ->getOption(array(
        'view',
        'extent',
      ), ''),
      '#states' => array(
        'visible' => array(
          ':input[name="options[view][limit_extent]"]' => array(
            'value' => 'custom',
          ),
        ),
      ),
    );
    $form['options']['misc'] = array(
      '#type' => 'fieldset',
      '#title' => t('Miscellaneous options'),
    );
    $form['options']['misc']['renderer'] = array(
      '#type' => 'radios',
      '#title' => t('Renderer'),
      '#description' => t('Renderer by default. Canvas, DOM and WebGL renderers are tested for support in that order. Note that at present only the Canvas renderer support vector data.'),
      '#options' => array(
        'canvas' => t('Canvas'),
        'dom' => t('DOM'),
        'webgl' => t('WebGL'),
      ),
      '#default_value' => $this
        ->getOption('renderer', 'canvas'),
      '#parents' => array(
        'options',
        'renderer',
      ),
    );
    $i = 0;
    $data = array();
    $map_options = $this
      ->getOptions();

    /** @var \Drupal\openlayers\Types\Base $object */
    foreach ($this
      ->getCollection()
      ->getFlatList() as $object) {
      $weight = 0;
      if (isset($map_options['capabilities']['options']['table'][$object
        ->getMachineName()])) {
        $weight = array_search($object
          ->getMachineName(), array_keys($map_options['capabilities']['options']['table']));
      }
      $data[$object
        ->getMachineName()] = array(
        'name' => $object
          ->getName(),
        'machine_name' => $object
          ->getMachineName(),
        'text' => isset($map_options['capabilities']['options']['table'][$object
          ->getMachineName()]) ? $map_options['capabilities']['options']['table'][$object
          ->getMachineName()] : $object
          ->getPluginDescription(),
        'weight' => $weight,
        'enabled' => isset($map_options['capabilities']['options']['table'][$object
          ->getMachineName()]) ? TRUE : FALSE,
        'default' => 1,
      );
    }
    uasort($data, function ($a, $b) {
      if ($a['enabled'] > $b['enabled']) {
        return -1;
      }
      elseif ($a['enabled'] < $b['enabled']) {
        return 1;
      }
      if ($a['weight'] < $b['weight']) {
        return -1;
      }
      elseif ($a['weight'] > $b['weight']) {
        return 1;
      }
      return 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'],
              '#attributes' => array(
                'class' => array(
                  'entry-order-weight',
                ),
              ),
            ),
          ),
          array(
            'data' => array(
              '#type' => 'hidden',
              '#default_value' => $entry['machine_name'],
            ),
          ),
          array(
            'data' => array(
              '#type' => 'checkbox',
              '#title' => t('Enable'),
              '#title_display' => 'invisible',
              '#default_value' => $entry['enabled'],
            ),
          ),
          array(
            'data' => array(
              '#type' => 'textfield',
              '#title' => t('Text'),
              '#title_display' => 'invisible',
              '#default_value' => $entry['text'],
              '#maxlength' => 256,
            ),
          ),
          check_plain($entry['name']),
          check_plain($entry['machine_name']),
        ),
        'class' => array(
          'draggable',
        ),
      );

      // Build rows of the form elements in the table.
      $row_elements[$id] = array(
        'weight' => &$rows[$id]['data'][1]['data'],
        'machine_name' => &$rows[$id]['data'][2]['data'],
        'enabled' => &$rows[$id]['data'][3]['data'],
        'text' => &$rows[$id]['data'][4]['data'],
      );
    }
    $form['options']['capabilities'] = array(
      '#type' => 'fieldset',
      '#title' => 'Map description and capabilities',
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
      'enabled' => array(
        '#type' => 'checkbox',
        '#title' => t('Enable map capabilities ?'),
        '#default_value' => (bool) $this
          ->getOption(array(
          'capabilities',
        ), FALSE),
      ),
      'options' => array(
        '#type' => 'container',
        '#states' => array(
          'visible' => array(
            ':input[name="options[capabilities][enabled]"]' => array(
              'checked' => TRUE,
            ),
          ),
        ),
        'container_type' => array(
          '#type' => 'select',
          '#title' => t('Container type'),
          '#options' => array(
            'fieldset' => 'Fieldset',
            'container' => 'Simple div',
          ),
          '#default_value' => $this
            ->getOption(array(
            'capabilities',
            'options',
            'container_type',
          ), 'fieldset'),
        ),
        'title' => array(
          '#type' => 'textfield',
          '#title' => t('Title'),
          '#description' => t('Show a title ? Empty to disable.'),
          '#default_value' => $this
            ->getOption(array(
            'capabilities',
            'options',
            'title',
          ), t('Map capabilities')),
          '#states' => array(
            'visible' => array(
              ':input[name="options[capabilities][options][container_type]"]' => array(
                'value' => 'fieldset',
              ),
            ),
          ),
        ),
        'description' => array(
          '#type' => 'textfield',
          '#title' => t('Description'),
          '#description' => t('Show a description ? Empty to disable.'),
          '#default_value' => $this
            ->getOption(array(
            'capabilities',
            'options',
            'description',
          ), t('Description')),
          '#states' => array(
            'visible' => array(
              ':input[name="options[capabilities][options][container_type]"]' => array(
                'value' => 'fieldset',
              ),
            ),
          ),
        ),
        'collapsible' => array(
          '#type' => 'checkbox',
          '#title' => t('Collapsible'),
          '#default_value' => (bool) $this
            ->getOption(array(
            'capabilities',
            'options',
            'collapsible',
          ), TRUE),
          '#states' => array(
            'visible' => array(
              ':input[name="options[capabilities][options][container_type]"]' => array(
                'value' => 'fieldset',
              ),
            ),
          ),
        ),
        'collapsed' => array(
          '#type' => 'checkbox',
          '#title' => t('Collapsed'),
          '#default_value' => (bool) $this
            ->getOption(array(
            'capabilities',
            'options',
            'collapsed',
          ), TRUE),
          '#states' => array(
            'visible' => array(
              ':input[name="options[capabilities][options][container_type]"]' => array(
                'value' => 'fieldset',
              ),
            ),
          ),
        ),
      ),
    );

    // Add the table to the form.
    $form['options']['capabilities']['options']['table'] = array(
      '#theme' => 'table',
      // The row form elements need to be processed and build,
      // therefore pass them as element children.
      'elements' => $row_elements,
      '#header' => array(
        // We need two empty columns for the weigth field and the cross.
        array(
          'data' => NULL,
          'colspan' => 2,
        ),
        array(
          'data' => t('Enabled'),
          'colspan' => 2,
        ),
        array(
          'data' => t('Description'),
          'colspan' => 1,
        ),
        t('Name'),
        t('Machine name'),
      ),
      '#rows' => $rows,
      '#empty' => t('There are no entries available.'),
      '#attributes' => array(
        'id' => 'entry-order-objects',
      ),
    );
    drupal_add_tabledrag('entry-order-objects', 'order', 'sibling', 'entry-order-weight');
  }

  /**
   * {@inheritdoc}
   */
  public function optionsFormSubmit(array $form, array &$form_state) {

    // So we can use the map API instead of working with arrays.
    parent::optionsFormSubmit($form, $form_state);
    $limit_extent = $this
      ->getOption(array(
      'view',
      'limit_extent',
    ), '');
    $extent = $this
      ->getOption(array(
      'view',
      'extent',
    ), '');
    if (empty($limit_extent) || $limit_extent == 'custom' && empty($extent)) {
      unset($form_state['values']['options']['view']['extent']);
      unset($form_state['values']['options']['view']['limit_extent']);
    }
    $capabilities = array();
    if (isset($form_state['values']['options']['capabilities']['enabled']) && (bool) $form_state['values']['options']['capabilities']['enabled'] == TRUE) {
      $elements = (array) $form_state['values']['options']['capabilities']['options']['table']['elements'];
      uasort($elements, function ($a, $b) {
        return $a['weight'] - $b['weight'];
      });
      foreach ($elements as $data) {
        if ((bool) $data['enabled'] == TRUE && !empty($data['text'])) {
          $capabilities[$data['machine_name']] = $data['text'];
        }
      }
      $form_state['values']['options']['capabilities']['options']['table'] = $capabilities;
    }
    else {
      $this
        ->clearOption('capabilities');
      unset($form_state['values']['options']['capabilities']);
    }
    parent::optionsFormSubmit($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function attached() {
    return array_merge_recursive(parent::attached(), Openlayers::getAttached());
  }

}

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::$options protected property The array containing the options. 6
Base::addObject public function Add an object into the collection of the parent object. Overrides ObjectInterface::addObject
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::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::i18nStringsRefresh public function Refresh string translations. Overrides ObjectInterface::i18nStringsRefresh 1
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::optionsFormValidate public function Validation callback for the options form. Overrides ObjectInterface::optionsFormValidate
Base::postBuild public function Invoked after an objects render array is built. Overrides ObjectInterface::postBuild 13
Base::preBuild public function Invoked before an objects render array is built. Overrides ObjectInterface::preBuild 4
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
Map::addComponent public function Overrides MapInterface::addComponent
Map::addControl public function Overrides MapInterface::addControl
Map::addInteraction public function Overrides MapInterface::addInteraction
Map::addLayer public function Overrides MapInterface::addLayer
Map::build public function Build render array of a map. Overrides MapInterface::build
Map::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 Base::getJS
Map::getSize public function Return the size of the map. Overrides MapInterface::getSize
Map::getTarget public function Get the target ID in which this map is rendered. Overrides MapInterface::getTarget
Map::isAsynchronous public function Whether or not this object has to be processed asynchronously. Overrides Base::isAsynchronous
Map::optionsToObjects public function Return a flat array containing Openlayers Objects from the options array. Overrides Base::optionsToObjects
Map::removeComponent public function Overrides MapInterface::removeComponent
Map::removeControl public function Overrides MapInterface::removeControl
Map::removeInteraction public function Overrides MapInterface::removeInteraction
Map::removeLayer public function Overrides MapInterface::removeLayer
Map::render public function Render a build array into HTML. Overrides MapInterface::render
Map::setSize public function Set the size of the map. Overrides MapInterface::setSize
Map::setTarget public function Set the target element to render this map into. Overrides MapInterface::setTarget
OLMap::attached public function Returns a list of attachments for building the render array. Overrides Map::attached
OLMap::optionsForm public function @TODO What is this return? If it is the form, why is form by reference? Overrides Base::optionsForm
OLMap::optionsFormSubmit public function Submit callback for the options form. Overrides Base::optionsFormSubmit