diff --git a/js/entity_browser.view.js b/js/entity_browser.view.js index 45c68ec..921bebd 100644 --- a/js/entity_browser.view.js +++ b/js/entity_browser.view.js @@ -53,22 +53,29 @@ // display over javascript event. Currently only multistep display // supports that functionality. if (drupalSettings.entity_browser_widget.auto_select) { - views_instance.$view.find('tr') + var selection_cells = views_instance.$view.find('.views-field-entity-browser-select'); + + // Register on cell parents (rows) click event. + selection_cells.parent() .once('register-row-click') .click(function (event) { event.preventDefault(); var $row = $(this); - var $input = $row.find('input'); + // Ensure to use input (checkbox) field from entity browser + // column dedicated for selection checkbox. + var $input = $row.find('.views-field-entity-browser-select input.form-checkbox'); + + // Get selection display element and trigger adding of entity + // over ajax request. $row.parents('form') .find('.entities-list') .trigger('add-entities', [[$input.val()]]); }); - // Hide column with checkboxes. - views_instance.$view.find('.views-field-entity-browser-select') - .hide(); + // Hide selection cells (selection column) with checkboxes. + selection_cells.hide(); } } } diff --git a/src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php b/src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php index 39abd3b..b943143 100644 --- a/src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php +++ b/src/Plugin/EntityBrowser/SelectionDisplay/MultiStepDisplay.php @@ -291,8 +291,9 @@ class MultiStepDisplay extends SelectionDisplayBase { $last_entity_id = $selected_entities[$selected_entity_keys[$key_index]]->id(); $html = $renderer->render($form['selection_display']['selected']['items_' . $last_entity_id . '_' . $selected_entity_keys[$key_index]]); + $ajax->addCommand( - new ReplaceCommand('div[id="' . $entity_pair_info['proxy_id'] . '"]', trim($html)) + new ReplaceCommand('div[id="' . $entity_pair_info['proxy_id'] . '"]', static::trimSingleHtmlTag($html)) ); $key_index++; @@ -305,11 +306,11 @@ class MultiStepDisplay extends SelectionDisplayBase { // Order is important, since commands are executed one after another. $ajax->addCommand( - new AfterCommand('.entities-list', trim($renderer->render($form['selection_display']['show_selection']))) + new AfterCommand('.entities-list', static::trimSingleHtmlTag($renderer->render($form['selection_display']['show_selection']))) ); $ajax->addCommand( - new AfterCommand('.entities-list', trim($renderer->render($form['selection_display']['use_selected']))) + new AfterCommand('.entities-list', static::trimSingleHtmlTag($renderer->render($form['selection_display']['use_selected']))) ); } } @@ -325,6 +326,34 @@ class MultiStepDisplay extends SelectionDisplayBase { } /** + * Make HTML with single tag suitable for Ajax response. + * + * Comments will be removed and also whitespace characters, because Ajax JS + * "insert" command handling checks number of base elements in response and + * wraps it in a "div" tag if there are more then one base element. + * + * @param string $html + * HTML content. + * + * @return string + * Returns cleaner HTML content, suitable for Ajax responses. + */ + protected static function trimSingleHtmlTag($html) { + $clearHtml = trim($html); + + // Remove comments around main single HTML tag. RegEx flag 's' is there to + // allow matching on whitespaces too. That's needed, because generated HTML + // contains a lot newlines. + if (preg_match_all('/(<(?!(!--)).+((\\/)|(<\\/[a-z]+))>)/is', $clearHtml, $matches)) { + if (!empty($matches) && !empty($matches[0])) { + $clearHtml = $matches[0][0]; + } + } + + return $clearHtml; + } + + /** * Submit callback for remove buttons. * * @param array $form