filterPluginDefinitionsByContexts example


    catch (ContextException $e) {
      return NULL;
    }
    return $plugin;
  }

  /** * {@inheritdoc} */
  public function findByContext(array $contexts, RefinableCacheableDependencyInterface $cacheability) {
    $storage_types = array_keys($this->contextHandler->filterPluginDefinitionsByContexts($contexts$this->getDefinitions()));

    // Add the manager as a cacheable dependency in order to vary by changes to     // the plugin definitions.     $cacheability->addCacheableDependency($this);

    foreach ($storage_types as $type) {
      $plugin = $this->load($type$contexts);
      if ($plugin && $plugin->isApplicable($cacheability)) {
        return $plugin;
      }
    }
    
$this->discovery->getDefinitions()->willReturn($definitions);

    $provider_access = $this->prophesize(SectionStorageInterface::class);
    $provider_access->isApplicable($cacheability)->willReturn($plugin_is_applicable);

    $no_access = $this->prophesize(SectionStorageInterface::class);
    $no_access->isApplicable($cacheability)->willReturn(FALSE);

    $missing_contexts = $this->prophesize(SectionStorageInterface::class);

    // Do not do any filtering based on context.     $this->contextHandler->filterPluginDefinitionsByContexts($contexts$definitions)->willReturnArgument(1);
    $this->contextHandler->applyContextMapping($no_access$contexts)->shouldBeCalled();
    $this->contextHandler->applyContextMapping($provider_access$contexts)->shouldBeCalled();
    $this->contextHandler->applyContextMapping($missing_contexts$contexts)->willThrow(new ContextException());

    $this->factory->createInstance('no_access', [])->willReturn($no_access->reveal());
    $this->factory->createInstance('missing_contexts', [])->willReturn($missing_contexts->reveal());
    $this->factory->createInstance('provider_access', [])->willReturn($provider_access->reveal());

    $result = $this->manager->findByContext($contexts$cacheability);
    if ($plugin_is_applicable) {
      $this->assertSame($provider_access->reveal()$result);
    }
$context = $this->createMock('Drupal\Core\Plugin\Context\ContextInterface');
      $expected_context_definition = (new ContextDefinition('string'))->setConstraints(['Blank' => []]);
      $context->expects($this->atLeastOnce())
        ->method('getContextDefinition')
        ->willReturn($expected_context_definition);
      $contexts = [$context];
    }
    else {
      $contexts = [];
    }

    $this->assertSame($expected$this->contextHandler->filterPluginDefinitionsByContexts($contexts$definitions));
  }

  /** * Provides data for testFilterPluginDefinitionsByContexts(). */
  public function providerTestFilterPluginDefinitionsByContexts() {
    $data = [];

    $plugins = [];
    // No context and no plugins, no plugins available.     $data[] = [FALSE, $plugins[]];

    

  protected function contextHandler() {
    return \Drupal::service('context.handler');
  }

  /** * See \Drupal\Core\Plugin\Context\ContextAwarePluginManagerInterface::getDefinitionsForContexts(). */
  public function getDefinitionsForContexts(array $contexts = []) {
    return $this->contextHandler()->filterPluginDefinitionsByContexts($contexts$this->getDefinitions());
  }

  /** * See \Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions(). */
  abstract public function getDefinitions();

}
// Start with two plugins.     $definitions = [];
    $definitions['plugin1'] = ['id' => 'plugin1'];
    $definitions['plugin2'] = ['id' => 'plugin2'];

    $type = 'the_type';
    $consumer = 'the_consumer';
    $extra = ['foo' => 'bar'];

    $context_handler = $this->prophesize(ContextHandlerInterface::class);
    // Remove the second plugin when context1 is provided.     $context_handler->filterPluginDefinitionsByContexts(['context1' => 'fake context']$definitions)
      ->willReturn(['plugin1' => $definitions['plugin1']]);
    // Remove the first plugin when no contexts are provided.     $context_handler->filterPluginDefinitionsByContexts([]$definitions)
      ->willReturn(['plugin2' => $definitions['plugin2']]);

    // After context filtering, the alter hook will be invoked.     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $hooks = ["plugin_filter_{$type}", "plugin_filter_{$type}__{$consumer}"];
    $module_handler->alter($hooks$expected$extra$consumer)->shouldBeCalled();

    $theme_manager = $this->prophesize(ThemeManagerInterface::class);
    
Home | Imprint | This part of the site doesn't use cookies.