You are here

abstract class Base in Openlayers 7.3

Class Base.

Hierarchy

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

Expanded class hierarchy of Base

1 file declares its use of Base
Error.php in src/Types/Error.php
Contains class Error.

File

src/Types/Base.php, line 18
Class Object.

Namespace

Drupal\openlayers\Types
View source
abstract class Base extends PluginBase implements ObjectInterface {

  /**
   * A unique ID for the object.
   *
   * @var string
   */
  protected $id;

  /**
   * The array containing the options.
   *
   * @var array
   */
  protected $options = array();

  /**
   * Holds the Collection object.
   *
   * @var Collection
   */
  protected $collection;

  /**
   * Holds all the attachment used by this object.
   *
   * @var array
   */
  protected $attached = array(
    'js' => array(),
    'css' => array(),
    'library' => array(),
    'libraries_load' => array(),
  );

  /**
   * {@inheritdoc}
   */
  public function init() {
    $this->options = $this
      ->getOptions();
    $this
      ->setWeight(0);
    return $this
      ->initCollection();
  }

  /**
   * {@inheritdoc}
   */
  public function getOptions() {
    if (!empty($this->options)) {
      return $this->options;
    }
    else {
      $configuration = $this
        ->getConfiguration();
      if (!empty($configuration['options'])) {
        return $configuration['options'];
      }
    }
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function setOptions(array $options = array()) {
    $this->options = $options;

    // Invalidate the Collection so it gets rebuilt with new options.
    $this->collection = NULL;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function getMachineName() {
    $configuration = $this
      ->getConfiguration();
    if (isset($configuration['machine_name'])) {
      return check_plain($configuration['machine_name']);
    }
    else {
      return 'undefined';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getName() {
    $configuration = $this
      ->getConfiguration();
    if (isset($configuration['name'])) {
      return check_plain($configuration['name']);
    }
    else {
      return 'undefined';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    $configuration = $this
      ->getConfiguration();
    if (isset($configuration['description'])) {
      return check_plain($configuration['description']);
    }
    else {
      return 'undefined';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function dependencies() {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function setFactoryService($factory_service) {
    $this->configuration['factory_service'] = $factory_service;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getFactoryService() {
    $configuration = $this
      ->getConfiguration();
    if (isset($configuration['factory_service'])) {
      return check_plain($configuration['factory_service']);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function initCollection() {
    if (is_null($this->collection) || !$this->collection instanceof Collection) {
      $this->collection = \Drupal::service('openlayers.Types')
        ->createInstance('Collection');
    }
    $this
      ->getCollection()
      ->import($this
      ->optionsToObjects());
    $this
      ->getCollection()
      ->append($this);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getCollection() {
    return $this->collection;
  }

  /**
   * {@inheritdoc}
   */
  public function optionsToObjects() {
    return array();
  }

  /**
   * {@inheritdoc}
   */
  public function addObject(ObjectInterface $object) {
    $this
      ->setOption($object
      ->getType() . 's', $this
      ->getOption($object
      ->getType() . 's', array()) + array(
      $object
        ->getMachineName(),
    ));
    $object
      ->setWeight(count($this
      ->getOption($object
      ->getType() . 's', array())) + 2);
    $this
      ->getCollection()
      ->import(array(
      $object,
    ));
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setOption($parents, $value = NULL) {
    $ref =& $this->options;
    if (is_string($parents)) {
      $parents = array(
        $parents,
      );
    }
    foreach ($parents as $parent) {
      if (isset($ref) && !is_array($ref)) {
        $ref = array();
      }
      $ref =& $ref[$parent];
    }
    $ref = $value;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getOption($parents, $default_value = NULL) {
    if (is_string($parents)) {
      $parents = array(
        $parents,
      );
    }
    if (is_array($parents)) {
      $notfound = FALSE;
      if (!isset($this->options)) {
        $notfound = TRUE;
        $parents = array();
        $options = array();
      }
      else {
        $options = $this->options;
      }
      foreach ($parents as $parent) {
        if (isset($options[$parent])) {
          $options = $options[$parent];
        }
        else {
          $notfound = TRUE;
          break;
        }
      }
      if (!$notfound) {
        return $options;
      }
    }
    if (is_null($default_value)) {
      return FALSE;
    }
    return $default_value;
  }

  /**
   * {@inheritdoc}
   */
  public function removeObject($object_machine_name) {
    $this
      ->getCollection()
      ->remove($object_machine_name);
    foreach (Openlayers::getPluginTypes() as $type) {
      $type .= 's';
      $objects = $this
        ->getOption($type, array());
      $objects = array_combine(array_values($objects), array_values($objects));
      unset($objects[$object_machine_name]);
      if (!empty($objects)) {
        $this
          ->setOption($type, array_values($objects));
      }
      else {
        $this
          ->clearOption($type);
      }
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   *
   * @TODO What is this return? If it is the form, why is form by reference?
   */
  public function optionsForm(array &$form, array &$form_state) {
    return array();
  }

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

  /**
   * {@inheritdoc}
   */
  public function optionsFormSubmit(array $form, array &$form_state) {
    if (isset($form_state['values']['options'])) {
      $options = array_merge((array) $this
        ->getOptions(), (array) $form_state['values']['options']);
      $this
        ->setOptions($options);
    }
    $form_state['item'] = $this
      ->getExport();

    // Refresh translatable strings.
    $this
      ->i18nStringsRefresh();
  }

  /**
   * {@inheritdoc}
   */
  public function getExport() {
    $configuration = $this
      ->getConfiguration();
    $options = $this
      ->getOptions();
    $options = Openlayers::array_map_recursive('\\Drupal\\openlayers\\Openlayers::floatval_if_numeric', (array) $options);
    $options = Openlayers::removeEmptyElements((array) $options);
    $configuration['options'] = $options;
    return (object) $configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function i18nStringsRefresh() {
  }

  /**
   * {@inheritdoc}
   */
  public function preBuild(array &$build, ObjectInterface $context = NULL) {
    foreach ($this
      ->getCollection()
      ->getFlatList() as $object) {
      if ($object !== $this) {
        $object
          ->preBuild($build, $context);
      }
    }
    drupal_alter('openlayers_object_preprocess', $build, $this);
  }

  /**
   * {@inheritdoc}
   */
  public function postBuild(array &$build, ObjectInterface $context = NULL) {
    foreach ($this
      ->getCollection()
      ->getFlatList() as $object) {
      if ($object !== $this) {
        $object
          ->postBuild($build, $context);
      }
    }
    drupal_alter('openlayers_object_postprocess', $build, $this);
  }

  /**
   * {@inheritdoc}
   */
  public function clearOption($parents) {
    $ref =& $this->options;
    if (is_string($parents)) {
      $parents = array(
        $parents,
      );
    }
    $last = end($parents);
    reset($parents);
    foreach ($parents as $parent) {
      if (isset($ref) && !is_array($ref)) {
        $ref = array();
      }
      if ($last == $parent) {
        unset($ref[$parent]);
      }
      else {
        if (isset($ref[$parent])) {
          $ref =& $ref[$parent];
        }
        else {
          break;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function resetCollection() {
    $this->collection = NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function attached() {
    if ($plugin = $this
      ->getPluginDefinition()) {
      $path = $this
        ->getClassDirectory();
      $jsdir = $path . '/js';
      $cssdir = $path . '/css';
      if (file_exists($jsdir)) {
        foreach (file_scan_directory($jsdir, '/.*\\.js$/') as $file) {
          $this->attached['js'][$file->uri] = array(
            'data' => $file->uri,
            'type' => 'file',
            'group' => Config::get('openlayers.js_css.group'),
            'weight' => Config::get('openlayers.js_css.weight'),
          );
        }
      }
      if (file_exists($cssdir)) {
        foreach (file_scan_directory($cssdir, '/.*\\.css$/') as $file) {
          $this->attached['css'][$file->uri] = array(
            'data' => $file->uri,
            'type' => 'file',
            'group' => Config::get('openlayers.js_css.group'),
            'weight' => Config::get('openlayers.js_css.weight'),
            'media' => Config::get('openlayers.js_css.media'),
          );
        }
      }
    }
    return $this->attached;
  }

  /**
   * {@inheritdoc}
   */
  public function getClassDirectory() {
    $class = explode('\\', $this->pluginDefinition['class']);
    return drupal_get_path('module', $this
      ->getProvider()) . '/src/' . implode('/', array_slice($class, 2, -1));
  }

  /**
   * {@inheritdoc}
   */
  public function getProvider() {
    $class = explode('\\', $this->pluginDefinition['class']);
    return $class[1];
  }

  /**
   * {@inheritdoc}
   */
  public function getObjects($type = NULL) {
    return array_values($this
      ->getCollection()
      ->getObjects($type));
  }

  /**
   * {@inheritdoc}
   */
  public function getParents() {
    return array_filter(Openlayers::loadAll('Map'), function ($map) {
      return array_filter($map
        ->getObjects($this
        ->getType()), function ($object) {
        return $object
          ->getMachineName() == $this
          ->getMachineName();
      });
    });
  }

  /**
   * {@inheritdoc}
   */
  public function getType() {
    $class = explode('\\', get_class($this));
    return drupal_strtolower($class[3]);
  }

  /**
   * {@inheritdoc}
   */
  public function getClassPath() {
    $class = explode('\\', $this->pluginDefinition['class']);
    return drupal_get_path('module', $this
      ->getProvider()) . '/src/' . implode('/', array_slice($class, 2)) . '.php';
  }

  /**
   * {@inheritdoc}
   */
  public function getDependencies() {
    $objects = $this
      ->getCollection()
      ->getFlatList();
    unset($objects[$this
      ->getType() . '_' . $this
      ->getMachineName()]);
    return $objects;
  }

  /**
   * {@inheritdoc}
   */
  public function isAsynchronous() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   *
   * !Attention! This function will remove any option that is named after a
   * plugin type e.g.: layers, controls, styles, interactions, components .
   */
  public function getJS() {
    $export = $this
      ->getExport();
    array_map(function ($type) use ($export) {
      unset($export->options[$type . 's']);
    }, Openlayers::getPluginTypes());
    $js = array(
      'mn' => $export->machine_name,
      'fs' => $export->factory_service,
    );
    if (!empty($export->options)) {
      $js['opt'] = $export->options;
    }
    return $js;
  }

  /**
   * {@inheritdoc}
   */
  public function getWeight() {
    return floatval($this->configuration['weight']);
  }

  /**
   * {@inheritdoc}
   */
  public function setWeight($weight) {
    $this->configuration['weight'] = floatval($weight);
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginDescription() {
    $plugin_definition = $this
      ->getPluginDefinition();
    return isset($plugin_definition['description']) ? $plugin_definition['description'] : '';
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    if (!isset($this->id)) {
      $css_name = drupal_clean_css_identifier($this
        ->getType() . '-' . $this
        ->getMachineName());

      // Use uniqid to ensure we've really an unique id - otherwise there will
      // occur issues with caching.
      $this->id = drupal_html_id('openlayers-' . $css_name . '-' . uniqid('', TRUE));
    }
    return $this->id;
  }

  /**
   * {@inheritdoc}
   */
  public function setId($id) {
    $this->id = drupal_html_id(drupal_clean_css_identifier($id));
    return $this;
  }

}

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::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::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::isAsynchronous public function Whether or not this object has to be processed asynchronously. Overrides ObjectInterface::isAsynchronous 3
Base::optionsForm public function @TODO What is this return? If it is the form, why is form by reference? Overrides ObjectInterface::optionsForm 54
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::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