Comments

borisson_ created an issue. See original summary.

borisson_’s picture

Status: Active » Needs review
StatusFileSize
new874 bytes
drunken monkey’s picture

Hm, interesting. I guess if we do this, we should either rename the display plugin to just "Views" (not "Views Page"), or create two new plugins for blocks and REST displays. (I guess I'd prefer the second variant – and with some small change, they could still all use the same deriver.)
Also, while supporting REST displays makes perfect sense, I'd say blocks are a bit of a special case for a "search display" – specifically, because they have no fixed path. We even have a getPath() method on the interface (though I don't know why that's not getUrl()), currently with no option to return NULL – what should that be for blocks?
I guess it can make sense, on a site, to define a search in a block, and then of course you might still want facets (or other stuff, e.g., autocompletion) for that search – but then we probably have to re-think display plugins a bit. At the very least, that one method. Which is really a pity, with the module now being in Beta, but I guess that can't be helped.

borisson_’s picture

We resolved this in facets like this, I just noticed that we don't do anything for rest displays.

  /**
   * {@inheritdoc}
   */
  public function getPath() {
    $display = View::load($this->pluginDefinition['view_id'])->getDisplay($this->pluginDefinition['view_display']);
    switch ($display['display_plugin']) {
      case 'page':
        $view = Views::getView($this->pluginDefinition['view_id']);
        $view->setDisplay($this->pluginDefinition['view_display']);
        return '/' . $view->getDisplay()->getPath();

      case 'block':
      default:
        return \Drupal::service('path.current')->getPath();
    }
  }

I guess seperate plugins make sense, I can make that change tonight.

drunken monkey’s picture

We resolved this in facets like this, I just noticed that we don't do anything for rest displays.

OK, I guess that will work fine for facets functionality in most cases. And really nothing better we can do there, I'd say (except spy into the block settings to see if it's enabled only for a certain path – but that's hacky, unreliable and just overkill, I'd say).
However, for search display plugins, I think just returning the current path there is pretty misleading. If you want to, e.g., show a list of all known search pages, it would have a link to that list for block displays. Being able to just return NULL seems to make more sense to me in that case. Just have to deal with that case in your Search API facet source plugin, I guess.

borisson_’s picture

Status: Needs review » Needs work

Ok, so for this issue to be in a better state we should:

  • Create a different plugin for block display type
  • Create a different plugin for rest display type
  • Change the interface to allow returning NULL as well as a string for the getPath() method
  • Create an issue to deprecate getPath and use getUrl instead
  • Apply that patch to facets to see if that resolves all outstanding issues, report problems there and here if needed.

I can work on those things tonight, anything else I need to do?

drunken monkey’s picture

  • Change the interface to allow returning NULL as well as a string for the getPath() method
  • Create an issue to deprecate getPath and use getUrl instead

getPath() doesn't return a string but a URL. That's why I want to rename it. But yeah, I guess we should deprecate it instead and remove it only in RC phase.

Otherwise, yeah, seems like this should do it. Thanks!
Do you already have a solution for isRenderedInCurrentRequest() for blocks?

borisson_’s picture

  /**
   * {@inheritdoc}
   */
  public function isRenderedInCurrentRequest() {
    $display = View::load($this->pluginDefinition['view_id'])->getDisplay($this->pluginDefinition['view_display']);
    switch ($display['display_plugin']) {
      case 'rest_export':
      case 'page':
        $request = \Drupal::requestStack()->getMasterRequest();
        if ($request->attributes->get('_controller') === 'Drupal\views\Routing\ViewPageController::handle') {
          list(, $search_api_view_id, $search_api_view_display) = explode(':', $this->getPluginId());

          if ($request->attributes->get('view_id') == $search_api_view_id && $request->attributes->get('display_id') == $search_api_view_display) {
            return TRUE;
          }
        }
        return FALSE;

      case 'block':
        // There is no way to know if a block is embedded on a page, because
        // blocks can be rendered in isolation (see big_pipe, esi, ...). To be
        // sure we're not disclosing information we're not sure about, we always
        // return false.
        return FALSE;
    }
    return FALSE;
  }
borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new4.21 KB
new4.21 KB

Add rest + block plugins.

borisson_’s picture

StatusFileSize
new540 bytes
new9.88 KB
drunken monkey’s picture

Component: Facets » Views integration
StatusFileSize
new9.03 KB
new9.4 KB

Thanks a lot!
Here are a few style changes plus nit-picking, but generally this already looks quite good.

I'm wondering, though, whether we can keep the static caching as-is in the deriver, with $this->derivatives? Seems like that would cache the derivatives for one of the three display types and then return them for the other two, too. Did you test whether this really works like this? Otherwise, we'll probably have to go back (cf. #2758321: Clean up deriver code) to keying the cached definitions by base plugin ID, too.

If you have some time, could you maybe also add tests for this to the Views test? I guess you'd just need to add a block and a REST display to the view and check they have corresponding search display plugins, too.

borisson_’s picture

If you have some time, could you maybe also add tests for this to the Views test? I guess you'd just need to add a block and a REST display to the view and check they have corresponding search display plugins, too.

Sure, I have time set aside tonight to work on this and #2794745: Use Search API's display plugin to fix facets, so I'll start by adding test coverage for this.

borisson_’s picture

StatusFileSize
new2.73 KB
new16.46 KB

This adds the requested tests.

Status: Needs review » Needs work

The last submitted patch, 13: support_block_and_rest-2799475-13.patch, failed testing.

borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new666 bytes
new12.46 KB

Add rest module to failing tests.

borisson_’s picture

StatusFileSize
new2.37 KB
new13.06 KB

I added a failing test to the viewstest to show that this work as expected yet.

Status: Needs review » Needs work

The last submitted patch, 16: support_block_and_rest-2799475-16.patch, failed testing.

borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new645 bytes
new13.1 KB

Status: Needs review » Needs work

The last submitted patch, 18: support_block_and_rest-2799475-18.patch, failed testing.

borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new742 bytes
new18.47 KB

Tests should be green now.

Status: Needs review » Needs work

The last submitted patch, 20: support_block_and_rest-2799475-20.patch, failed testing.

nick_vh’s picture

  1. +++ b/src/Plugin/search_api/display/ViewsDisplayDeriver.php
    @@ -0,0 +1,88 @@
    +      foreach ($all_views as $view) {
    

    foreach{ if { foreach { if { while {

    This is pretty complex to read. Can't we simplify this a bit so this becomes easier to read and easier to maintain?

  2. +++ b/src/Plugin/search_api/display/ViewsDisplayDeriver.php
    @@ -0,0 +1,88 @@
    +              // Make sure the machine name is unique. (Will almost always be
    

    This is pretty scary to read :) So what if this is suddenly the case? Should we add a testcase for this?

borisson_’s picture

Status: Needs work » Needs review
Issue tags: +Dublin2016
StatusFileSize
new6.87 KB
new19.59 KB

Fixed failing test by adding rest as dependency + also fixed Nick's remark (with his help - here at DrupalCon).

Status: Needs review » Needs work

The last submitted patch, 23: support_block_and_rest-2799475-23.patch, failed testing.

borisson_’s picture

Status: Needs work » Needs review
StatusFileSize
new3.13 KB
new20.06 KB
borisson_’s picture

StatusFileSize
new5.9 KB
new20.38 KB

Sorry, the interdiff also includes the changes from #25. But this is another cleanup.

nick_vh’s picture

Status: Needs review » Reviewed & tested by the community

Tagging RTBC. Because this is one of the last blockers to get FacetAPI back up and running I vote to get this committed unless someone shouts really loud with a clear reason why not to do this.

  • Nick_vh committed e5c059e on 8.x-1.x authored by borisson_
    Issue #2799475 by borisson_, drunken monkey: Support block and rest...
nick_vh’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

drunken monkey’s picture

Status: Closed (fixed) » Needs review
StatusFileSize
new4.15 KB

Thanks for fixing this, and sorry I wasn't there to help.
Just have a few minor code style changes/corrections to propose for this.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

No worries, we all need some time off every once in a while :)

I agree with the changes - they look good.

  • drunken monkey committed 71978bc on 8.x-1.x
    Follow-up to #2799475 by drunken monkey: Fixed a few code style issues.
    
drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Good to hear, thanks for reviewing!
Committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.