0

i´m building a new nap view style. I get the map shown correctly, but only without the data of the view :-(.

class OpenlayersViews extends StylePluginBase {
  /**
   * Whether the display allows the use of a pager or not.
   *
   * @var bool
   */
  protected $usesPager = FALSE;

  /**
   * Whether the display allows the use of a 'more' link or not.
   *
   * @var bool
   */
  protected $usesMore = FALSE;

  /**
   * Whether the display allows area plugins.
   *
   * @var bool
   */
  protected $usesAreas = FALSE;

  /**
   * Does the style plugin support grouping of rows.
   */
  protected $usesGrouping = FALSE;

  /**
   * Does the style plugin for itself support to add fields to it's output.
   */
  protected $usesFields = TRUE;


  /**
   * {@inheritdoc}
   */
  public function evenEmpty() {
    // Render map even if there is no data.
    return TRUE;
  }

  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['openlayers_map'] = array('default' => 'none selected');
    return $options;
  }

   /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $openlayers_map_options = [];
    foreach (openlayers_get_map() as $key => $map) {
      $openlayers_map_options[$key] = $this->t($map['label']);
    }

    $form['openlayers_map'] = [
      '#title' => $this->t('OpenLayers Map'),
      '#type' => 'select',
      '#options' => $openlayers_map_options,
      '#default_value' => $this->options['openlayers_map'],
      '#required' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    dsm("OpenlayersViews:render");
    $map = openlayers_get_map($this->options['openlayers_map']);
    $mapid = Html::getUniqueId('openlayers_map');
    $features = array();

    /*
     * Here i need a function to get the result of the view
     */

    $element = openlayers_render_map($mapid, $map, $features, FALSE);
    return $element;
  }  
}

I do not know, if render is the right position for getting the data. So maybe some of you can explain, what is the next step to do.

best regards

0

Normally you would iterate through the rows and call a row plugin to render the field values. As I understand the question you want to access the field values directly by row index and field id?

Then try StylePluginBase::getFieldValue:

$value = $this->getFieldValue($index, $field);

Normally a style plugin iterates the rows like this:

  public function render() {
    if (empty($this->view->rowPlugin)) {
      trigger_error('Missing row plugin', E_WARNING);
      return [];
    }
    $rows = [];

    foreach ($this->view->result as $row_index => $row) {
      $this->view->row_index = $row_index;
      $rows[] = $this->view->rowPlugin->render($row);
    }
    ...

For this to work you have to set protected $usesRowPlugin = TRUE; and configure a row plugin which renders the map.


If you want to do all rendering in the style plugin you can use the same loop and get the field values directly:

  public function render() {
    $rows = [];
    foreach ($this->view->result as $index => $row) {
      $rows[] = render_map(
        $this->getFieldValue($index, 'field1'),
        $this->getFieldValue($index, 'field2')
      );
    }
    ...
  • thanks, of course but how can i get the row index? I want to iterate through the rows and build an other array. – AndyLicht Jun 4 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.