Currently we have a conditional in the Style plugin that checks the field alias, and only displays the rendered field value if it's a unknown alias. This is a bit backwards, we can simplfy this logic by ALWAYS calling render() on the field value but add an option to the row options (to go with the alias option) to output the raw values. This can then be checked when render time comes around, and the raw/rendered output will depend on this and not if the alias is unknown or not.

This patch also creates a table for the row plugin options instead, imo this is much better. I have also added some more tests for this.

I'm looking for feedback on what we need to label these are for the table etc... as well as reviews obviously :)

Comments

damiankloip’s picture

So some initial thoughts from the code side:

  • Not sure 'field options' is a good name for the options I'm storing?
  • There might be a better property name than rawOutput?
  • Is it worth moving the static extractFromOptionsArray() method I created somewhere else?
damiankloip’s picture

StatusFileSize
new12.21 KB

Sorry, this patch, the first one has some changes to FieldPluginBase that should have been removed from the patch.

I will also remove the dupe comments in the new tests in the next patch.

damiankloip’s picture

StatusFileSize
new2.79 KB
damiankloip’s picture

Sorry, Ignore that, posted on the wrong issue! #2 is good

Status: Needs review » Needs work

The last submitted patch, 1929014-3.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review

Nice work!

+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -41,14 +41,25 @@ class DataFieldRow extends RowPluginBase {
+  protected $rawOutput = array();

What about simply rawOutputOptions?

+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -41,14 +41,25 @@ class DataFieldRow extends RowPluginBase {
+      $aliases = static::extractFromOptionsArray('alias', $options);

Nice method!

+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -116,14 +137,14 @@ public function render($row) {
+        $value = $field->sanitizeValue($row->{$field->field_alias}, 'xss_admin');

I'm totally fine with making sanitizeValue a public method.

Additional you probably want to use $field->getValue ?

+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -149,4 +170,21 @@ public function getFieldKeyAlias($id) {
+  public static function extractFromOptionsArray($key, $options) {

I'm wondering whether this should be protected instead? I don't see so much use-case for other people here?

+++ b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.phpundefined
@@ -211,4 +199,30 @@ public function testUIFieldAlias() {
+    $row_options = 'admin/structure/views/nojs/display/test_serializer_display_field/rest_export_1/row_options';
...
+    $view->setDisplay('ws_endpoint_1');

Is there a reason why we use two different displays (rest_export_1 for the UI and ws_endpoint_1 for the test?)

+++ b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.phpundefined
@@ -211,4 +199,30 @@ public function testUIFieldAlias() {
+    $this->drupalPost($row_options, array('row_options[field_options][created][raw_output]' => '1'), t('Apply'));

Oh good to know that you actually want to have that option on the row plugin?

+++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.phpundefined
@@ -458,6 +458,7 @@ protected function defineOptions() {
+    $options['raw_output'] = array('default' => TRUE);

Can't we still have that option on the row plugin? It feels a lot like that would confuse people. Additional, can you think of use-cases where you really want to have this behavior, as the user don't have any clue what the "raw" output could be, and even worse, we probably can't provide one for fieldapi fields. I'm sorry this changed was probably not intendend

damiankloip’s picture

StatusFileSize
new3.04 KB
new12.38 KB

Thanks for the review! I have added your points to the patch.

The ws_endpoint_1 display doesn't actually exist anymore, that's actually a mistake from earlier iterations of the rest export patch :/ Which leads me to look at the logic in ViewExecutabel::setDisplay().... I think we should change that slightly.

dawehner’s picture

There are just these two small issues.

+++ b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.phpundefined
@@ -211,4 +199,30 @@ public function testUIFieldAlias() {
+      $this->assertIdentical($values['created'], $view->result[$index]->views_test_data_created, format_string('Expected raw created value found.'));

Any reason to use format_string without variables? In general I'm wondering whether it really makes sense to compare against $view->result or should better just do one single $this->assertEqual($this->drupalGetAJAX() ... , $expected))

+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -116,14 +137,14 @@ public function render($row) {
+      // If this is not unknown and the raw output option has been set, just
+      // get the raw value.

We can put the "get" into the previous line.

+++ b/core/modules/rest/lib/Drupal/rest/Tests/Views/StyleSerializerTest.phpundefined
@@ -211,4 +199,30 @@ public function testUIFieldAlias() {
+      $this->assertIdentical($values['created'], $view->result[$index]->views_test_data_created, format_string('Expected raw created value found.'));

Any reason to use format_string without variables? In general I'm wondering whether it really makes sense to compare against $view->result or should better just do one single $this->assertEqual($this->drupalGetAJAX() ... , $expected))

damiankloip’s picture

StatusFileSize
new1.65 KB
new12.36 KB

Thanks for the review! I have made those changes, except for changing how the test works. It makes sense to test this by iterating as we have different keys v array/object to test against, so it may be more confusing to get an array of both values first then check?

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Nice refactoring and better and more tests, nice!

xjm’s picture

#9: 1929014-9.patch queued for re-testing.

catch’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -149,4 +170,21 @@ public function getFieldKeyAlias($id) {
+  protected static function extractFromOptionsArray($key, $options) {
+    return array_map(function($item) use ($key) {
+      return array_key_exists($key, $item) ? $item[$key] : NULL;
+    }, $options);

Can't this be isset()? If the array value is NULL, it'll end up returning NULL either way.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new665 bytes
new12.35 KB

Yes, I guess you will get the same outcome either way :) and isset() is more performant anyway.

dawehner’s picture

+++ b/core/modules/rest/lib/Drupal/rest/Plugin/views/row/DataFieldRow.phpundefined
@@ -183,7 +183,7 @@ public function getFieldKeyAlias($id) {
+      return isset($key, $item) ? $item[$key] : NULL;

I don't think this is the right code :) Shouldn't it be isset($item[$key])?

damiankloip’s picture

StatusFileSize
new654 bytes
new12.35 KB

HA, yep ....

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Well in fact isset($a, $b, $c) works, oh php :)

damiankloip’s picture

Yeah, it will 'work'. Doesn't help us at all here though. You have got to love php for these things :)

catch’s picture

Status: Reviewed & tested by the community » Fixed

Thanks! Committed/pushed to 8.x.

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Updated issue summary.