1

I have created a custom field that is a collection of a couple things, one being a taxonomy entity reference. Now I want to be able to filter a view based on this taxonomy reference. With the code below I see the correct entity reference and can find the correct terms and everything looks good until I click save.

Then it throws this giant AJAX error that i cannot make heads or tails of.

ajax.js?v=8.1.3:965 Uncaught AjaxError:

An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /admin/structure/views/view/category_news_block_6/preview/music
StatusText: OK
ResponseText: 'Technology (25)''Technology (25)'[{"command":"settings","settings":{"ajaxPageState":{"theme":"onsetadmin","libraries":"abheading\/abheading,archived_favorites\/ajax_saving,classy\/base,classy\/messages,classy\/node,contextual\/drupal.contextual-links,contextual\/drupal.contextual-links,contextual\/drupal.contextual-toolbar,core\/drupal.active-link,core\/drupal.autocomplete,core\/drupal.collapse,core\/drupal.collapse,core\/drupal.dialog.ajax,core\/drupal.dialog.ajax,core\/drupal.states,core\/drupal.tabledrag,core\/drupal.tableresponsive,core\/html5shiv,core\/jquery.ui.tabs,core\/normalize,event_calendar\/event_calendar_lib,find\/find_lib,ga_report\/ga_send,import\/ncs,login_mod\/logout,my_podcast\/podcast-js,ncsdev\/ipad_fixes,onsetadmin\/bootstrap,onsetadmin\/global-styling,poll\/drupal.poll-links,scroll_menu\/scroll,seven\/global-styling,shortcut\/drupal.shortcut,social_media\/social_media_style,socialmedianode\/socialmedianode_style,toolbar\/toolbar,toolbar\/toolbar.escapeAdmin,tour\/tour,urltracking\/urltracking_form_style,user\/drupal.user.icons,video\/video_form,views\/views.module,views_ui\/views_ui.admin","theme_token":"gEoIQ537BMQtSpWSiqEYebhCssHSDCwaCPLN1224XqM"},"ajaxTrustedUrl":{"\/admin\/structure\/views\/view\/category_news_block_6\/preview\/music":true},"ajax":{"preview-submit":{"url":"\/admin\/structure\/views\/view\/category_news_block_6\/preview\/music","wrapper":"views-preview-wrapper","event":"click","progress":{"type":"fullscreen"},"method":"replaceWith","disable-refocus":true,"dialogType":"ajax","submit":{"_triggering_element_name":"op","_triggering_element_value":"Update preview"}}},"pluralDelimiter":"\u0003","user":{"uid":"1","permissionsHash":"fd4956e871fce9817761b65924d9442c46e3c597742b03fc3169b17173425acb"}},"merge":true},{"command":"insert","method":null,"selector":null,"data":"\u003Cdiv id=\u0022views-preview-wrapper\u0022 class=\u0022views-preview-wrapper views-admin clearfix\u0022\u003E\u003Cform class=\u0022view-preview-form view-form\u0022 data-drupal-selector=\u0022views-ui-preview-form\u0022 action=\u0022\/admin\/structure\/views\/view\/category_news_block_6\/preview\/music\u0022 method=\u0022post\u0022 id=\u0022views-ui-preview-form\u0022 accept-charset=\u0022UTF-8\u0022\u003E\n  \u003Ch2 class=\u0022view-preview-form__title\u0022\u003EPreview\u003C\/h2\u003E\u003Cdiv class=\u0022js-form-item form-item js-form-type-checkbox form-type-checkbox js-form-item-live-preview form-item-live-preview\u0022\u003E\n        \u003Cinput data-drupal-selector=\u0022edit-displays-live-preview\u0022 type=\u0022checkbox\u0022 id=\u0022edit-displays-live-preview\u0022 name=\u0022live_preview\u0022 value=\u00221\u0022 checked=\u0022checked\u0022 class=\u0022form-checkbox\u0022 \/\u003E\n\n        \u003Clabel for=\u0022edit-displays-live-preview\u0022 class=\u0022option\u0022\u003EAuto preview\u003C\/label\u003E\n      \u003C\/div\u003E\n\u003Cdiv class=\u0022js-form-item form-item js-form-type-textfield form-type-textfield js-form-item-view-args form-item-view-args\u0022\u003E\n      \u003Clabel for=\u0022preview-args\u0022\u003EPreview with contextual filters:\u003C\/label\u003E\n        \u003Csection id=\u0022auth-button\u0022\u003E\u003C\/section\u003E\n\u003Csection id=\u0022view-selector-1\u0022\u003E\u003C\/section\u003E\n\n\n\u003Cinput data-drupal-selector=\u0022preview-args\u0022 aria-describedby=\u0022preview-args--description\u0022 type=\u0022text\u0022 id=\u0022preview-args\u0022 name=\u0022view_args\u0022 value=\u0022\u0022 size=\u002260\u0022 maxlength=\u0022128\u0022 class=\u0022form-text\u0022 \/\u003E\n\n\n            \u003Cdiv id=\u0022preview-args--description\u0022 class=\u0022description\u0022\u003E\n      Separate contextual filter values with a \u0022\/\u0022. For example, \u003Cem class=\u0022placeholder\u0022\u003E40\/12\/10\u003C\/em\u003E.\n    \u003C\/div\u003E\n  \u003C\/div\u003E\n\u003Cinput data-drupal-selector=\u0022form-gdz-2l46lu60yf2rt2cblxxvbeck-cnw41cgteidqo\u0022 type=\u0022hidden\u0022 name=\u0022form_build_id\u0022 value=\u0022form--GDZ_2l46LU60Yf2rT2CblxXvbeCk_cNw41cgtEIdqo\u0022 \/\u003E\n\u003Cinput data-drupal-selector=\u0022edit-view-preview-form-form-token-wquwcx8kkz4\u0022 type=\u0022hidden\u0022 name=\u0022form_token\u0022 value=\u0022nOZgTdYSYFgrtq57O-sekR4qqFbs4c02928-_rVCKrE\u0022 \/\u003E\n\u003Cinput data-drupal-selector=\u0022edit-view-preview-form-rudggtaedzm\u0022 type=\u0022hidden\u0022 name=\u0022form_id\u0022 value=\u0022view_preview_form\u0022 \/\u003E\n\u003Cdiv id=\u0022preview-submit-wrapper\u0022 class=\u0022preview-submit-wrapper form-actions js-form-wrapper form-wrapper\u0022 data-drupal-selector=\u0022edit-actions\u0022\u003E\u003Cinput class=\u0022arguments-preview button js-form-submit form-submit\u0022 data-drupal-selector=\u0022preview-submit\u0022 data-disable-refocus=\u0022true\u0022 type=\u0022submit\u0022 id=\u0022preview-submit\u0022 name=\u0022op\u0022 value=\u0022Update preview\u0022 \/\u003E\n\u003C\/div\u003E\n\u003Cdiv id=\u0022views-live-preview\u0022 class=\u0022views-live-preview js-form-wrapper form-wrapper\u0022 data-drupal-selector=\u0022edit-preview\u0022\u003E\u003Cdiv class=\u0022views-query-info\u0022\u003E\u003Ctable data-drupal-selector=\u0022edit-table\u0022 id=\u0022edit-table--AKkl1_P_Zwc\u0022 data-striping=\u00221\u0022\u003E\n  \n  \n  \n    \u003C\/table\u003E\n\u003C\/div\u003EUnable to preview due to validation errors.\u003C\/div\u003E\n\n\u003C\/form\u003E\n\u003C\/div\u003E","settings":null},{"command":"insert","method":"prepend","selector":null,"data":"\n    \u003Cdiv role=\u0022contentinfo\u0022 aria-label=\u0022Error message\u0022 class=\u0022messages messages--error\u0022\u003E\n          \u003Cdiv role=\u0022alert\u0022\u003E\n                  \u003Ch2 class=\u0022visually-hidden\u0022\u003EError message\u003C\/h2\u003E\n                    The value  is not an array for in on filter: : Category\n                \u003C\/div\u003E\n      \u003C\/div\u003E\n    \n","settings":null}]Drupal.Ajax.error @ ajax.js?v=8.1.3:965complete @ ajax.js?v=8.1.3:510t.complete @ jquery.form.min.js?v=3.51:11j @ jquery.js:3099fireWith @ jquery.js:3211x @ jquery.js:8279(anonymous function) @ jquery.js:8605

Here is the code i am using for my filter.

<?php

namespace Drupal\my_custom_field\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\Core\Entity\Element\EntityAutocomplete;
use Drupal\views\Views;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;

/**
 * Filter handler for usernames.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("custom_field_category")
 */
class CustomCategoryFilter extends InOperator {


  protected function valueForm(&$form, FormStateInterface $form_state) {
    $form['value'] = array(
        '#title' => $this->t('Category'),
        '#type' => 'entity_autocomplete',
        '#selection_handler' => 'default:taxonomy_term',
        '#target_type' => 'taxonomy_term',
        '#selection_settings'=> [
            'target_bundle'=> array(
                'category',
            ),
        ],
    );
  }

}

and my hook

<?php

/**
 * implements hook_views_data_alter
 */
function priority_widget_views_data_alter(array &$data){
  $data['node__field_content_category']['priority_widget_category_filter'] = [
      'title' => t('Category'),
      'filter' => array(
          'title' => t('Category'),
          'help' => 'Searches the category field in the Priority Widget',
          'field' => 'field_content_category_target_id',
          'id' => 'priority_widget_category',
      ),

  ];
}

What is causing this ajax error? Or is there an easy way to debug this?

1

The significant line from that error is: The value is not an array for in on filter: : Category

If you grep in core for "is not an array for" you'll get modules/views/src/Plugin/views/filter/InOperator.php, specifically a line in its validate function that is testing $this->value to see if it's an array.

$errors[] = $this->t('The value @value is not an array for @operator on filter: @filter', array('@value' => var_export($this->value), '@operator' => $this->operator, '@filter' => $this->adminLabel(TRUE)));

Try to debug and find out if there's anywhere in your custom views filter that $this->value is equal to a string or something, and convert it to an array.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.