Under settings for the JSON output style, when attempting to use the multi-select form field for the Hexadecimal Encoding options (ENCODE TAGS, ENCODE APOSTROPHES) to add to the json_encode() call, value(s) selected do not persist after clicking "Apply".

The other checkbox options are correctly interpolated into the bitmask value for the optional [int] $options parameter for json_encode(). See: http://php.net/manual/en/function.json-encode.php

The specific options that are not recognized are:
JSON_HEX_TAG
JSON_HEX_APOS
JSON_HEX_QUOT
JSON_HEX_AMP

This is for 2 reasons:

#1: In views/plugins/views_plugin_style_json.inc - A mismatch in the array keys in option_definition() and options_form() for $options['encoding']['contains']['char_encoding'] and then later incorrectly used as $this->options['json_char_encoding'] instead of $this->options['char_encoding']

$form['char_encoding'] = array(
      '#type' => 'select',
      '#title' => t('Hexadecimal (base 16) encoding'),
      '#options' => array(
        'JSON_HEX_TAG' => t('Encode tags'),
        'JSON_HEX_APOS' => t('Encode apostrophe'),
        'JSON_HEX_QUOT' => t('Encode quotes'),
        'JSON_HEX_AMP' => t('Encode ampersand'),
      ),
      '#multiple' => TRUE,
      '#default_value' => $this->options['json_char_encoding'],
      '#description' => t('You can combine multiple options.'),
);

Code should be:

$form['char_encoding'] = array(
      '#type' => 'select',
      '#title' => t('Hexadecimal (base 16) encoding'),
      '#options' => array(
        'JSON_HEX_TAG' => t('Encode tags'),
        'JSON_HEX_APOS' => t('Encode apostrophe'),
        'JSON_HEX_QUOT' => t('Encode quotes'),
        'JSON_HEX_AMP' => t('Encode ampersand'),
      ),
      '#multiple' => TRUE,
      '#default_value' => $this->options['char_encoding'],
      '#description' => t('You can combine multiple options.'),
);

2 - In views/theme/view_view_json_style.theme.inc: The multi-select for $form[char_encoding] form field array is never iterated to build the bitmask.

$bitmask = NULL;
  foreach($bitmasks as $mask_key => $_bitmask) {
    if(isset($options[$mask_key]) && $options[$mask_key] && !is_array($options[$mask_key])) {
      $bitmask = $bitmask | constant($_bitmask['bitmask']);
    }
  }
  $vars['bitmask'] = $bitmask;

The problem is with the !is_array() call.
The $form[char_encoding] will be an array, as it is a multiple
form field.
So, the array is never included in the bitmask calculation.

Should be:

$bitmask = NULL;
  foreach($bitmasks as $mask_key => $_bitmask) {
    if(isset($options[$mask_key]) && $options[$mask_key]) {
      if(!is_array($options[$mask_key])){
        $bitmask = $bitmask | constant($_bitmask['bitmask']);
      }
      else{ //the multi-select array for Hexadecimal character encoding options from options array
        foreach($options[$mask_key] as $char_encode){
          $bitmask = $bitmask | constant($char_encode);  
        }
      }
    }
  }
  $vars['bitmask'] = $bitmask;

Steps to replicate:

1- Set up a view using JSON output style.
2- Under display style settings for JSON, select one (or many) of the Hexadecimal Encoding options in the multiple
form field.
3- Click Apply
4 - Revisit the settings, notice no value persists.

Am attaching patches in comments.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lookatthosemoose’s picture

davidneedham’s picture

Got a whitespace error because of the whitespace at the end of this line:

+          $bitmask = $bitmask | constant($char_encode);  

Re-rolled the patch with the whitespace removed. Works for me.

davidneedham’s picture

Issue summary: View changes

more clarification.

kenorb’s picture

Assigned: lookatthosemoose » Unassigned
ultrabob’s picture

#1973116: views_json: Error when accessing format settings in views_ui seems to contain the same patch to the plugin file. The two issues are probably related.

anthonylindsay’s picture

Status: Needs review » Needs work

I've committed the change from #1973116: views_json: Error when accessing format settings in views_ui , so that part of this issue is done.

I've been trying to understand what the second part of this issue does... I can apply the change, and dpm() will output values which change according to changes in settings, so that's fine too. But how do I know if the end result encoding is working? I'm not too up on hex encoding.

Once the patch is applied, what steps do I need to take to test it and create a positive result? What does successfully encoded content look like?

AdamGerthel’s picture

I'm having trouble with ampersands not converting properly to html entities and figured that the HEX settings would fix it. I tried the patch in order to get the settings to stick. They do stick now (yay!) but ampersands are not converted.

But how do I know if the end result encoding is working? I'm not too up on hex encoding.

Not sure either, but I didn't get the results I expected.

UPDATE: They are converted as expected. Try with a node title that contains "&".