You are here

class Field in Openlayers 7.3

Class Field.

Plugin annotation


@OpenlayersPlugin(
 id = "Field",
 description = "Provides a source where data are geocoded from provided options."
)

Hierarchy

  • class \Drupal\openlayers\Types\Base extends \Drupal\Component\Plugin\PluginBase implements ObjectInterface
    • class \Drupal\openlayers\Types\Source implements SourceInterface
      • class \Drupal\openlayers\Plugin\Source\Vector\Vector
        • class \Drupal\openlayers_field\Plugin\Source\Field\Field

Expanded class hierarchy of Field

File

modules/openlayers_field/src/Plugin/Source/Field/Field.php, line 20
Source: Field.

Namespace

Drupal\openlayers_field\Plugin\Source\Field
View source
class Field extends Vector {

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

  /**
   * {@inheritdoc}
   */
  public function optionsForm(array &$form, array &$form_state) {
    $geocoder_handlers = array();
    foreach (geocoder_handler_info() as $name => $handler) {
      $geocoder_handlers[$name] = $handler['title'];
    }
    $form['options']['geocoder_handler'] = array(
      '#type' => 'select',
      '#title' => t('Geocoder handler'),
      '#options' => $geocoder_handlers,
      '#required' => TRUE,
      '#default_value' => $this
        ->getOption('geocoder_handler', 'google'),
    );
    drupal_add_tabledrag('entry-order-geocoder-handlers', 'order', 'sibling', 'entry-order-weight');
    $form['options']['geocoder_cache'] = array(
      '#type' => 'select',
      '#title' => t('Geocoder cache type'),
      '#description' => t('Type of geocoder cache to use'),
      '#options' => array(
        0 => t('No caching'),
        1 => t('Static-cache but no persistent-cache'),
        2 => t('Both static-cache and persistent-cache'),
      ),
      '#default_value' => $this
        ->getOption('geocoder_cache', 0),
    );
    $fields = $this
      ->getOption('fields', array(
      array(),
    ));
    if (!empty($fields[0])) {
      $fields[] = array();
    }
    foreach ($fields as $index => $field) {
      $form['options']['fields'][$index] = array(
        '#type' => 'fieldset',
        '#title' => $field == end($fields) ? t('Add a new feature') : 'Feature ' . $index,
        '#collapsible' => TRUE,
        '#collapsed' => $field == end($fields) ? TRUE : FALSE,
        'title' => array(
          '#title' => 'Title',
          '#type' => 'textfield',
          '#default_value' => isset($fields[$index]['title']) ? $fields[$index]['title'] : '',
        ),
        'description' => array(
          '#title' => 'Description',
          '#type' => 'textarea',
          '#default_value' => isset($fields[$index]['description']) ? $fields[$index]['description'] : '',
        ),
        'address' => array(
          '#title' => 'Address',
          '#type' => 'textfield',
          '#default_value' => isset($fields[$index]['address']) ? $fields[$index]['address'] : '',
        ),
        'geojson' => array(
          '#type' => 'textarea',
          '#title' => 'GeoJson',
          '#disabled' => TRUE,
          '#default_value' => isset($fields[$index]['geojson']) ? $fields[$index]['geojson'] : '',
        ),
      );
    }
  }

  /**
   * {@inheritdoc}
   */
  public function optionsFormSubmit(array $form, array &$form_state) {
    $fields = $form_state['values']['options']['fields'];
    $geocoder_handler = $form_state['values']['options']['geocoder_handler'];
    $geocoder_cache = $form_state['values']['options']['geocoder_cache'];

    // This is for optimizing the source rendering in JS.
    // It converts all the address fields of the source to WKT.
    foreach ($fields as $index => &$field) {
      if (isset($field['address']) && !empty($field['address'])) {
        $geocoder = geocoder($geocoder_handler, $field['address'], array(), $geocoder_cache);
        if (!is_null($geocoder)) {
          $field['wkt'] = $geocoder
            ->out('wkt');
        }
        else {
          unset($field['geojson']);
        }
      }
      else {
        unset($fields[$index]);
      }
    }
    $form_state['values']['options']['fields'] = $fields;
    parent::optionsFormSubmit($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function getJS() {
    $js = parent::getJS();
    $features = $this
      ->getGeojsonFeatures();
    if (!empty($features)) {
      $js['opt']['geojson_data'] = array(
        'type' => 'FeatureCollection',
        'features' => $features,
      );
    }
    unset($js['opt']['fields']);
    return $js;
  }

  /**
   * Compute the GeoJSON features array.
   *
   * @return array
   *   The geojson array.
   */
  protected function getGeojsonFeatures() {
    $features = array();
    foreach ($this
      ->getOption('fields', array()) as $field) {
      $feature = FALSE;
      if (isset($field['geojson']) && !empty($field['geojson'])) {
        $feature = json_decode($field['geojson'], TRUE);
        $json = FALSE;
        if (isset($field['wkt']) && !empty($field['wkt'])) {
          geophp_load();
          $geophp = \geoPHP::load($field['wkt'], 'wkt');
          if (is_object($geophp)) {
            $json = $geophp
              ->out('json');
          }
        }
        else {
          if (isset($field['address']) && !empty($field['address'])) {
            $geocoder = geocoder($this
              ->getOption('geocoder_handler', 'google'), $field['address'], array(), $this
              ->getOption('geocoder_cache', 2));
            if (is_object($geocoder)) {
              $json = $geocoder
                ->out('json');
            }
          }
        }
        if ($feature) {
          $features[] = $feature;
        }
      }
      return $features;
    }
  }

}

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::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::isAsynchronous public function Whether or not this object has to be processed asynchronously. Overrides ObjectInterface::isAsynchronous 3
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
Field::dependencies public function Defines dependencies. Overrides Base::dependencies
Field::getGeojsonFeatures protected function Compute the GeoJSON features array.
Field::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
Field::optionsForm public function @TODO What is this return? If it is the form, why is form by reference? Overrides Base::optionsForm
Field::optionsFormSubmit public function Submit callback for the options form. Overrides Base::optionsFormSubmit
Source::$options protected property The array containing the options. Overrides Base::$options