Spun off of #2574077: REST Export display cannot show any raw output for fields using the Field field handler, dependent on commits from that issue

Problem/Motivation

If you have a view, add a REST Export display, use the Field field handler, and set "raw output" on a reference field (ie File, Image, Content, etc.), it always outputs "null" instead of an actual value.

Basically, this:

Broken View settings

Causes this:

    {
        "title": "Test Page title",
        "field_test_image": null,
        "field_test_content_reference": null,
        "field_test_taxonomy_reference": null
    }

Proposed resolution

I think the issue stems from FieldItemBase::__get.

  public function __get($name) {
    // There is either a property object or a plain value - possibly for a
    // not-defined property. If we have a plain value, directly return it.
    if (isset($this->properties[$name])) {
      return $this->properties[$name]->getValue();
    }
    elseif (isset($this->values[$name])) {
      return $this->values[$name];
    }
  }

Files have target_id's, not names, so they get to this point and break.

Remaining tasks

Fix it
Tests

User interface changes
API changes
Data model changes

Comments

charginghawk created an issue. See original summary.

charginghawk’s picture

Title: Entity reference fields are 'null', using REST Export display + Field field handler + raw output » Entity reference fields returning 'null', using Views + REST Export display + Field field handler + raw output
charginghawk’s picture

Just to share my temporary workaround for this issue...

What I'm doing to circumvent this is creating custom Fields format via plugin, ie \Drupal\mymodule\Plugin\views\row\MymoduleDataFieldRow that extends Drupal\rest\Plugin\views\row\DataFieldRow, changing the render function to something like...

  public function render($row) {
    $output = array();

    foreach ($this->view->field as $id => $field) {
      // If the raw output option has been set, just get the raw value.
      if (!empty($this->rawOutputOptions[$id])) {
        $value = $field->getValue($row);

        // We disturb this function for a very important reformatting.
        // Check whether field is field collection or image.
        $items = $row->_entity->$id;

        if ($id == 'field_mymodule_image' && !empty($items->first())) {
          $value = $this->formatImage($items); // Custom function to format images.
        }

        if ($id == 'field_mymodule_fieldcollection' && !empty($value)) {
          $value = $this->formatFieldCollection($items); // Custom function to format field collections.
        }
        // Now returning to the regular function.

      }
      // Otherwise, pass this through the field advancedRender() method.
      else {
        $value = $field->advancedRender($row);
      }

      $output[$this->getFieldKeyAlias($id)] = $value;
    }

    return $output;
  }
wim leers’s picture

Title: Entity reference fields returning 'null', using Views + REST Export display + Field field handler + raw output » REST views: using the 'raw_output' option on field-based REST views causes entity reference fields returning 'null'
Issue tags: -REST, -views +VDC, +Needs tests

So, this is when using \Drupal\rest\Plugin\views\row\DataFieldRow's raw_output field option.

vasi’s picture

I think this is happening due to \Drupal\views\Plugin\views\field\Field::getValue():

    if ($field_item_definition->getFieldStorageDefinition()->getCardinality() == 1) {
      return $field ? $field_item_list->$field : $field_item_list->value;
    }

    $values = [];
    foreach ($field_item_list as $field_item) {
      $values[] = $field ? $field_item->$field : $field_item->value;
    }

Notice that if there is no 'value', such as with reference fields, this doesn't yield anything interesting. Even when 'value' exists, it may not be the only relevant property—eg: formatted text fields, where we might be interested in 'format' or 'summary' as well.

Does anything else ever call this method? The only thing I can find is in StylePluginBase::renderGrouping().

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

lykyd’s picture

StatusFileSize
new43.5 KB
new3.64 KB

This issue is still happening in Drupal 8.1.6
I'm using a Rest view and I'm trying to apply the "Raw Output" option to a Custom text field so it doesn't change my "var&var2" in "var&var2".
The issue also happens when I use any other field like "Content: Path" for example and decide to rewrite its output (Rewrite result -> Override the output).

It gives my NULL instead of printing my a raw rewrited value.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

Title: REST views: using the 'raw_output' option on field-based REST views causes entity reference fields returning 'null' » REST views: using DataFieldRow's "raw_output" option causes entity reference fields to output NULL
Issue tags: -Entity Reference
Related issues: +#1929014: Fix views rest export plugin render values and add option to output raw values

@damiankloip added the "raw output" capability to DataFieldRow in #1929014: Fix views rest export plugin render values and add option to output raw values and included test coverage at \Drupal\rest\Tests\Views\StyleSerializerTest::testFieldRawOutput()

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
quietone’s picture

Status: Active » Closed (outdated)
Issue tags: -Needs tests +Bug Smash Initiative

I tested this on 9.4.x and was not able to reproduce this error. And according to #10 this was fixed in #1929014: Fix views rest export plugin render values and add option to output raw values.

Therefore, closing as outdated

Thanks!

lawxen’s picture

I found the problem still exist on an site of Drupal9.5.3

lawxen’s picture