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
File
- src/
Types/ Base.php, line 18 - Class Object.
Namespace
Drupal\openlayers\TypesView 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
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:: |
protected | property | The array containing the options. | 6 |
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 |
Refresh string translations. Overrides ObjectInterface:: |
1 |
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 |
@TODO What is this return? If it is the form, why is form by reference? Overrides ObjectInterface:: |
54 |
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 |
Invoked before an objects render array is built. Overrides ObjectInterface:: |
4 |
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:: |