diff --git a/config/schema/search_api.views.schema.yml b/config/schema/search_api.views.schema.yml index b4e21ef2..6cfe0f00 100644 --- a/config/schema/search_api.views.schema.yml +++ b/config/schema/search_api.views.schema.yml @@ -179,6 +179,14 @@ views.field.search_api_numeric: type: string label: 'Singular/Plural string' +views.field.search_api_text: + type: views.field.search_api + label: 'Search API text' + mapping: + filter_type: + type: string + label: 'Output filter/sanitization to apply' + views.filter.search_api_boolean: type: views.filter.boolean label: 'Search API boolean' diff --git a/search_api.views.inc b/search_api.views.inc index a3ac7221..187555a4 100644 --- a/search_api.views.inc +++ b/search_api.views.inc @@ -735,7 +735,7 @@ function _search_api_views_get_field_handler_mapping() { ]; $text_mapping = [ - 'id' => 'search_api', + 'id' => 'search_api_text', 'filter_type' => 'xss', ]; $plain_mapping['field_item:text_long'] = $text_mapping; diff --git a/src/Plugin/views/field/SearchApiText.php b/src/Plugin/views/field/SearchApiText.php new file mode 100644 index 00000000..8e7a1505 --- /dev/null +++ b/src/Plugin/views/field/SearchApiText.php @@ -0,0 +1,67 @@ + !empty($this->definition['filter_type']) ? $this->definition['filter_type'] : 'plain', + ]; + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + parent::buildOptionsForm($form, $form_state); + + $args = [ + '@strip' => $this->t('Strip HTML tags'), + '@rewrite' => $this->t('Rewrite results'), + ]; + $form['filter_type'] = [ + '#title' => $this->t('Enable HTML in this field'), + '#type' => 'radios', + '#options' => [ + 'xss' => $this->t('Field value can contain HTML'), + 'xss_admin' => $this->t('Field value can contain HTML (even potentially unsafe tags)'), + 'plain' => $this->t('Do not allow HTML in this field'), + ], + '#default_value' => $this->options['filter_type'], + 'xss' => [ + '#description' => $this->t('This will allow the most common HTML tags, except unsafe ones.'), + ], + 'xss_admin' => [ + '#description' => $this->t('This will allow almost all tags (except scripts and styles). Use with caution.'), + ], + 'plain' => [ + '#description' => $this->t('This will display any HTML tags in the field value as plain text. For instead removing those tags, use the "@strip" option under "@rewrite".', $args), + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function render_item($count, $item) { + return $this->sanitizeValue($item['value'], $this->options['filter_type']); + } + +}