Commit 8843239c authored by Steven Jones's avatar Steven Jones
Browse files

Issue #3522528 by steven jones, xperd: Allow specifying the XML encoding property

parent dabf0613
Loading
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -127,6 +127,9 @@ views.style.data_export:
      type: mapping
      label: 'XML settings'
      mapping:
        encoding:
          type: string
          label: 'Encoding'
        root_node_name:
          type: string
          label: 'Root node name'
+6 −0
Original line number Diff line number Diff line
@@ -817,11 +817,17 @@ class DataExport extends RestExport {
    if ($output_format == 'xml') {
      $format_options = $view->getStyle()->options['xml_settings'];
      $root_node_name = $format_options['root_node_name'];
      $encoding = $format_options['encoding'];
      $maximum = $export_limit ? $export_limit : $total_rows;

      // Remove xml declaration, root node opening tag.
      if ($context['sandbox']['progress'] != 0) {
        if (!empty($encoding)) {
          $string = str_replace('<?xml version="1.0" encoding="' . $encoding . '"?>', '', $string);
        }
        else {
          $string = str_replace('<?xml version="1.0"?>', '', $string);
        }
        $string = str_replace("<{$root_node_name}>", '', $string);
      }

+17 −0
Original line number Diff line number Diff line
@@ -86,6 +86,7 @@ class DataExport extends Serializer {
    ];

    $options['xml_settings']['contains'] = [
      'encoding' => ['default' => 'UTF-8'],
      'root_node_name' => ['default' => 'response'],
      'item_node_name' => ['default' => 'item'],
    ];
@@ -201,9 +202,20 @@ class DataExport extends Serializer {
          $xml_options = $this->options['xml_settings'];
          // Add our default options for backwards compatibility.
          $xml_options += [
            'encoding' => 'UTF-8',
            'root_node_name' => 'response',
            'item_node_name' => 'item',
          ];
          $form['xml_settings']['encoding'] = [
            '#type' => 'select',
            '#title' => $this->t('Encoding'),
            '#default_value' => $xml_options['encoding'],
            '#options' => [
              '' => $this->t('None specified'),
              'UTF-8' => $this->t('UTF-8'),
            ],
          ];
          // @todo widen this regex to support all possible XML tags, while making sure it can still work in the browser.
          $xml_tag_regex = '[A-Za-z_][A-Za-z0-9_.:\\-]*';
          $form['xml_settings']['root_node_name'] = [
            '#type' => 'textfield',
@@ -379,6 +391,7 @@ class DataExport extends Serializer {
    $options = $this->options['xml_settings'] ?? [];
    // Add our default options for backwards compatibility.
    $options += [
      'encoding' => 'UTF-8',
      'root_node_name' => 'response',
      'item_node_name' => 'item',
    ];
@@ -390,6 +403,10 @@ class DataExport extends Serializer {
      // @todo We can rely on the constant being defined once we drop support for Drupal 9.
      $context[defined('XmlEncoder::ROOT_NODE_NAME') ? XmlEncoder::ROOT_NODE_NAME : 'xml_root_node_name'] = $options['root_node_name'];
    }
    if ($options['encoding']) {
      // @todo We can rely on the constant being defined once we drop support for Drupal 9.
      $context[defined('XmlEncoder::ENCODING') ? XmlEncoder::ENCODING : 'xml_encoding'] = $options['encoding'];
    }

    $item_node_name = $options['item_node_name'] ?: 'item';
    if ($item_node_name != 'item') {
+84 −0
Original line number Diff line number Diff line
@@ -202,6 +202,40 @@ display:
      displays:
        page_1: page_1
        default: '0'
  xml_no_encoding:
    display_plugin: data_export
    id: xml_no_encoding
    display_title: xml
    position: null
    display_options:
      path: test/data_export/xml/no_encoding
      style:
        type: data_export
        options:
          formats:
            xml: xml
          xml_settings:
            encoding: ''
      displays:
        page_1: page_1
        default: '0'
  xml_utf8_encoding:
    display_plugin: data_export
    id: xml_utf8_encoding
    display_title: xml
    position: null
    display_options:
      path: test/data_export/xml/utf8_encoding
      style:
        type: data_export
        options:
          formats:
            xml: xml
          xml_settings:
            encoding: UTF-8
      displays:
        page_1: page_1
        default: '0'
  page_1:
    display_plugin: page
    id: page_1
@@ -217,3 +251,53 @@ display:
      row:
        type: entity:node
      path: test/data_export/page

  batched_no_encoding:
    display_plugin: data_export
    id: batched_no_encoding
    display_title: 'Data export'
    position: 2
    display_options:
      display_extenders: { }
      path: test/data_export/xml/batched/no_encoding
      filename: test_1
      automatic_download: false
      redirect_path: ''
      style:
        type: data_export
        options:
          formats:
            xml: xml
          xml_settings:
            encoding: ''
      export_method: batch
      export_batch_size: 3
      store_in_public_file_directory: true
      redirect_to_display: none
      custom_redirect_path: false
      include_query_params: false

  batched_utf8_encoding:
    display_plugin: data_export
    id: batched_utf8_encoding
    display_title: 'Data export'
    position: 3
    display_options:
      display_extenders: { }
      path: test/data_export/xml/batched/utf8_encoding
      filename: test_1
      automatic_download: false
      redirect_path: ''
      style:
        type: data_export
        options:
          formats:
            xml: xml
          xml_settings:
            encoding: UTF-8
      export_method: batch
      export_batch_size: 3
      store_in_public_file_directory: true
      redirect_to_display: none
      custom_redirect_path: false
      include_query_params: false
+3 −24
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@ display:
          hide_empty: false
          empty_zero: false
          settings:
            link_to_entity: true
            link_to_entity: false
          plugin_id: field
          relationship: none
          group_type: group
@@ -171,9 +171,9 @@ display:
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
  data_export_1:
  csv:
    display_plugin: data_export
    id: data_export_1
    id: csv
    display_title: 'Data export'
    position: 1
    display_options:
@@ -194,30 +194,9 @@ display:
            strip_tags: true
            trim: true
            encoding: utf8
          xls_settings:
            xls_format: Excel2007
            metadata:
              creator: ''
              last_modified_by: ''
              title: ''
              description: ''
              subject: ''
              keywords: ''
              category: ''
              manager: ''
              company: ''
      export_method: batch
      export_batch_size: 4
      store_in_public_file_directory: true
      redirect_to_display: none
      custom_redirect_path: false
      include_query_params: false
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - request_format
        - 'user.node_grants:view'
        - user.permissions
      tags: {  }
Loading