When using Services views runing on php 7.1 node title and text fields only return the first character, text area are empty.
I am using the plain text formatter for all text fields and text area's.

Comments

emerham created an issue. See original summary.

emerham’s picture

It does still work in 7.0 and 5.6.

Turning on drupal logging and loaded an view I am greeted with

Notice: Uninitialized string offset: 0 in services_views_execute_view() (line 258 of /sites/all/modules/services_views/services_views.resource.inc).

The call is on this section of code

257 if ($field->field_info['cardinality'] == 1 && count($obj) == 1) {
258   $obj = $obj[$idx];
259 }

so its on

emerham’s picture

My guess is it has something to do with how element ordering is assign in 7.1 that was different in previous versions when assigning by reference. http://php.net/manual/en/migration71.incompatible.php

I will keep digging and hopefully get it working as I want to move my code base to PHP 7.1 and we rely on this module.

emerham’s picture

StatusFileSize
new647 bytes

So the issue is how PHP handles the empty index operators. PHP Reference and when this is done the string is silently converted to an array of size 1 PHP 7.1 Array Update.

The fix ended up being simple, just needed to change the empty initiator from '' to array().

emerham’s picture

Status: Active » Needs review
pcambra’s picture

emerham’s picture

Status: Closed (duplicate) » Needs review
StatusFileSize
new21.36 KB
new141.77 KB

Close, but the issue is still with how array's are variables are created and when the variable is converted to an array from a string silently in PHP 7.1.1+
Before applying my patch ( and the linked issue) this is what my service returns:
service results before patching

After applying my patch this is what the service should be returning:
After patching, full results are returned

emerham’s picture

Assigned: Unassigned » emerham
StatusFileSize
new650 bytes

So I had a regression where empty strings were returning as empty arrays. By typecasting the string as an array I am able to get this working like it should on latest version of PHP 7 as well as PHP 5.

prakashsingh’s picture

Perfect. The patch at #8 works for me.

Thanks
Prakash

generalredneck’s picture

Status: Needs review » Postponed (maintainer needs more info)
StatusFileSize
new24.84 KB

@emerham,
I'm not able to reproduce this. I am running PHP 7.1.12-1+ubuntu16.04.1+deb.sury.org+1 under Drupal VM. Would you mind exporting your view?

I've tested with the following without your patch on the latest Dev as well as before #2718537: Notice: Uninitialized string offset: 0 in services_views_execute_view() was committed and I'm not able to reproduce this issue. Here's my view... and here is my content type. Many Text has multiple carnality.

$view = new view();
$view->name = 'services_views_sorted_nodes';
$view->description = 'Test View for Services Views';
$view->tag = 'testing';
$view->base_table = 'node';
$view->human_name = 'Services Views Sorted Nodes';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Sorted Nodes';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '5';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['title']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['title']['link_to_node'] = FALSE;
/* Field: Content: Nid */
$handler->display->display_options['fields']['nid']['id'] = 'nid';
$handler->display->display_options['fields']['nid']['table'] = 'node';
$handler->display->display_options['fields']['nid']['field'] = 'nid';
$handler->display->display_options['fields']['nid']['label'] = '';
$handler->display->display_options['fields']['nid']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['nid']['alter']['ellipsis'] = FALSE;
/* Field: Content: Text Field */
$handler->display->display_options['fields']['field_text_field']['id'] = 'field_text_field';
$handler->display->display_options['fields']['field_text_field']['table'] = 'field_data_field_text_field';
$handler->display->display_options['fields']['field_text_field']['field'] = 'field_text_field';
/* Field: Content: Body */
$handler->display->display_options['fields']['body']['id'] = 'body';
$handler->display->display_options['fields']['body']['table'] = 'field_data_body';
$handler->display->display_options['fields']['body']['field'] = 'body';
/* Field: Content: Many Text */
$handler->display->display_options['fields']['field_many_text']['id'] = 'field_many_text';
$handler->display->display_options['fields']['field_many_text']['table'] = 'field_data_field_many_text';
$handler->display->display_options['fields']['field_many_text']['field'] = 'field_many_text';
$handler->display->display_options['fields']['field_many_text']['delta_offset'] = '0';
/* Sort criterion: Content: Title */
$handler->display->display_options['sorts']['title']['id'] = 'title';
$handler->display->display_options['sorts']['title']['table'] = 'node';
$handler->display->display_options['sorts']['title']['field'] = 'title';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['defaults']['access'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'view own unpublished content';
$handler->display->display_options['path'] = 'sorted-nodes';

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->display->display_options['block_description'] = 'Sorted Nodes';

/* Display: Existing Service Display */
$handler = $view->new_display('services', 'Existing Service Display', 'services_1');
$handler->display->display_options['path'] = 'existing-service-display';

article

generalredneck’s picture

Status: Postponed (maintainer needs more info) » Needs review

Ok I managed to stumble across the problem. This is yet another difference between the "Services View Resource" using a services views display type and just using the straight up views endpoint. I should have noticed when you had Capital letters in your labels... My bad... Let me do some more testing.

generalredneck’s picture

Status: Needs review » Fixed

@emerham,
I made a slight change. I like the readability of
$output[$index]->$target_key[] = '';
vs
$output[$index]->$target_key = (array) '';

I actually had to do a test on the 2nd to see that casting a string to an array actually made an array with the single string element in it... which goes against my intuition in C based laguages where strings are an array of chars...

Example I expected this to be equivalent to $output[$index]->$target_key = array(); because it was an empty string. Instead we get an array with one element: '' in it. Likewise if you do $output[$index]->$target_key = (array) "BLAH"; I expected to see an array of 4 elements 'B', 'L', 'A', and 'H'... not an array with one element "BLAH" in it...

I'm still giving you full credit for the fix though as it was your initial work that found it...

This is all necessary because down in the output assignment, we always assign the output to $obj[$idx] instead of checking to see if the cardinality is 1. The fix to the notice before in #2718537: Notice: Uninitialized string offset: 0 in services_views_execute_view() was covering this up really by checking if this was an array... when in theory, we should always be dealing with arrays of values...

Status: Fixed » Closed (fixed)

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

WayneDyck’s picture

What is the process for getting this commit pushed out as an update to the stable branch?

dmitrii’s picture

note: $output[$index]->$target_key[] = ''; breaks sites on PHP 5.6 with message Fatal error: Cannot use [] for reading in services_views/services_views.resource.inc

zuhair_ak’s picture

Maybe we should change it to
$output[$index]->$target_key = array(); for it to work?

dmitrii’s picture

StatusFileSize
new648 bytes

services_views-php-7_1-fixes-2910966-18.patch fixes issue with PHP 5.6 and works with PHP 7.1

dmitrii’s picture

@zuhair_ak It looks like we need value in the array to use same logic for multivalue and singlevalue fields.
It converted to a single value later at line 257

          // Check cardinality of the field.
          if ($field->field_info['cardinality'] == 1 && is_array($obj) && count($obj) == 1) {
            $obj = $obj[$idx];
          }
liam morland’s picture

@#17: To do the same thing, it would have to be like this:

$output[$index]->$target_key = array('');

I think this is more readable than the other options.

liam morland’s picture

I have made child ticket #3032803: Fatal error: Cannot use [] for reading to continue this.

scott.whittaker’s picture

Previous patch no longer applies, here's an update.

scott.whittaker’s picture

StatusFileSize
new792 bytes