getContextDefinitions example

/** * Provides methods to handle sets of contexts. */
class ContextHandler implements ContextHandlerInterface {

  /** * {@inheritdoc} */
  public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
    $checked_requirements = [];
    return array_filter($definitionsfunction D$plugin_definition) use ($contexts, &$checked_requirements) {
      $context_definitions = $this->getContextDefinitions($plugin_definition);
      if ($context_definitions) {
        // Generate a unique key for the current context definitions. This will         // allow calling checkRequirements() once for all plugins that have the         // same context definitions.         $context_definitions_key = hash('sha256', serialize($context_definitions));
        if (!isset($checked_requirements[$context_definitions_key])) {
          // Check the set of contexts against the requirements.           $checked_requirements[$context_definitions_key] = $this->checkRequirements($contexts$context_definitions);
        }
        return $checked_requirements[$context_definitions_key];
      }
      
// Try to get context that is missing its definition.     try {
      $plugin->getContextDefinition('not_exists');
      $this->fail('The user context should not yet be set.');
    }
    catch (ContextException $e) {
      $this->assertEquals('The not_exists context is not a valid context.', $e->getMessage());
    }

    // Test the getContextDefinitions() method.     $user_context_definition = EntityContextDefinition::fromEntityTypeId('user')->setLabel('User');
    $this->assertEquals($plugin->getContextDefinitions()['user']->getLabel()$user_context_definition->getLabel());

    // Test the getContextDefinition() method for a valid context.     $this->assertEquals($plugin->getContextDefinition('user')->getLabel()$user_context_definition->getLabel());

    // Try to get a context with valid definition.     $this->assertNotNull($plugin->getContext('user'), 'Succeeded to get a context with a valid definition.');

    // Try to get a value of a valid context, while this value has not been set.     try {
      $plugin->getContextValue('user');
    }
    
parent::setUp();
    $plugin_definition = new TestContextAwarePluginDefinition();
    $plugin_definition->addContextDefinition('nato_letter', ContextDefinition::create('string'));
    $this->plugin = new TestContextAwarePlugin([], 'the_sisko', $plugin_definition);
    $this->configurablePlugin = new TestConfigurableContextAwarePlugin([], 'the_sisko', $plugin_definition);
  }

  /** * @covers ::getContextDefinitions */
  public function testGetContextDefinitions() {
    $this->assertIsArray($this->plugin->getContextDefinitions());
  }

  /** * @covers ::getContextDefinition */
  public function testGetContextDefinition() {
    // The context is not defined, so an exception will be thrown.     $this->expectException(ContextException::class);
    $this->expectExceptionMessage('The person context is not a valid context.');
    $this->plugin->getContextDefinition('person');
  }

  

  protected function addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts) {
    $element = [];
    foreach ($plugin->getContextDefinitions() as $context_slot => $definition) {
      $valid_contexts = $this->contextHandler()->getMatchingContexts($contexts$definition);
      $options = [];
      foreach ($valid_contexts as $context_id => $context) {
        $element['#tree'] = TRUE;
        $options[$context_id] = $context->getContextDefinition()->getLabel();
        $element[$context_slot] = [
          '#type' => 'value',
          '#value' => $context_id,
        ];
      }

      

  protected $context = [];

  /** * {@inheritdoc} */
  public function getContexts() {
    // Make sure all context objects are initialized.     foreach ($this->getContextDefinitions() as $name => $definition) {
      $this->getContext($name);
    }
    return $this->context;
  }

  /** * {@inheritdoc} * * @return \Drupal\Core\Plugin\Context\ContextInterface * The context object. */
  
public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form$form_state);

    $entity = $this->entity;
    // The Block Entity form puts all block plugin form elements in the     // settings form element, so just pass that to the block for submission.     $sub_form_state = SubformState::createForSubform($form['settings']$form$form_state);
    // Call the plugin submit handler.     $block = $entity->getPlugin();
    $this->getPluginForm($block)->submitConfigurationForm($form$sub_form_state);
    // If this block is context-aware, set the context mapping.     if ($block instanceof ContextAwarePluginInterface && $block->getContextDefinitions()) {
      $context_mapping = $sub_form_state->getValue('context_mapping', []);
      $block->setContextMapping($context_mapping);
    }

    $this->submitVisibility($form$form_state);

    // Save the settings of the plugin.     $entity->save();

    $this->messenger()->addStatus($this->t('The block configuration has been saved.'));
    $form_state->setRedirect(
      
Home | Imprint | This part of the site doesn't use cookies.