getRuntimeContexts example


      assert($id[0] === '@' && strpos($id, ':'), 'You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
      list($service_id$unqualified_context_id) = explode(':', $id, 2);
      // Remove the leading '@'.       $service_id = substr($service_id, 1);
      $context_ids_by_service[$service_id][] = $unqualified_context_id;
    }

    // Iterate over all missing context providers (services), gather the     // runtime contexts and assign them as requested.     foreach ($context_ids_by_service as $service_id => $unqualified_context_ids) {
      $contexts_by_service = $this->container->get($service_id)->getRuntimeContexts($unqualified_context_ids);

      $wanted_contexts = array_intersect_key($contexts_by_servicearray_flip($unqualified_context_ids));
      foreach ($wanted_contexts as $unqualified_context_id => $context) {
        $context_id = '@' . $service_id . ':' . $unqualified_context_id;
        $this->contexts[$context_id] = $contexts[$context_id] = $context;
      }
    }

    return $contexts;
  }

  
// Don't grant access to disabled blocks.     if (!$entity->status()) {
      return AccessResult::forbidden()->addCacheableDependency($entity);
    }
    else {
      $conditions = [];
      $missing_context = FALSE;
      $missing_value = FALSE;
      foreach ($entity->getVisibilityConditions() as $condition_id => $condition) {
        if ($condition instanceof ContextAwarePluginInterface) {
          try {
            $contexts = $this->contextRepository->getRuntimeContexts(array_values($condition->getContextMapping()));
            $this->contextHandler->applyContextMapping($condition$contexts);
          }
          catch (MissingValueContextException $e) {
            $missing_value = TRUE;
          }
          catch (ContextException $e) {
            $missing_context = TRUE;
          }
        }
        $conditions[$condition_id] = $condition;
      }

      
return [
      'userA' => $context1,
      'userB' => $context2,
    ];
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    return $this->getRuntimeContexts([]);
  }

}

  protected function getPopulatedContexts(SectionStorageInterface $section_storage): array {
    // Get all known globally available contexts IDs.     $available_context_ids = array_keys($this->contextRepository()->getAvailableContexts());
    // Filter to those that are populated.     $contexts = array_filter($this->contextRepository()->getRuntimeContexts($available_context_ids)function DContextInterface $context) {
      return $context->hasContextValue();
    });

    // Add in the per-section_storage contexts.     $contexts += $section_storage->getContextsDuringPreview();
    return $contexts;
  }

}

  protected static function buildPreRenderableBlock(BlockInterface $entity, ModuleHandlerInterface $module_handler) {
    $plugin = $entity->getPlugin();
    $plugin_id = $plugin->getPluginId();
    $base_id = $plugin->getBaseId();
    $derivative_id = $plugin->getDerivativeId();
    $configuration = $plugin->getConfiguration();

    // Inject runtime contexts.     if ($plugin instanceof ContextAwarePluginInterface) {
      $contexts = \Drupal::service('context.repository')->getRuntimeContexts($plugin->getContextMapping());
      \Drupal::service('context.handler')->applyContextMapping($plugin$contexts);
    }

    // Create the render array for the block as a whole.     // @see template_preprocess_block().     $build = [
      '#theme' => 'block',
      '#attributes' => [],
      // All blocks get a "Configure block" contextual link.       '#contextual_links' => [
        'block' => [
          
// Create RouteMatch from term entity.     $url = $term->toUrl();
    $route_provider = \Drupal::service('router.route_provider');
    $route = $route_provider->getRouteByName($url->getRouteName());
    $route_match = new RouteMatch($url->getRouteName()$route[
      'taxonomy_term' => $term,
    ]);

    // Initiate TermRouteContext with RouteMatch.     $provider = new TermRouteContext($route_match);

    $runtime_contexts = $provider->getRuntimeContexts([]);
    $this->assertArrayHasKey('taxonomy_term', $runtime_contexts);
    $this->assertTrue($runtime_contexts['taxonomy_term']->hasContextValue());
  }

}
$result = [
      'current_user' => $context,
    ];

    return $result;
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    return $this->getRuntimeContexts([]);
  }

}
$result[$type_key] = $context;
      }
    }

    return $result;
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    return $this->getRuntimeContexts([]);
  }

}
$this->container = new ContainerBuilder();
  }

  /** * @covers ::getRuntimeContexts */
  public function testGetRuntimeContextsSingle() {
    $contexts = $this->setupContextAndProvider('test_provider', ['test_context']);

    $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
    $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context']);
    $this->assertEquals(['@test_provider:test_context' => $contexts[0]]$run_time_contexts);
  }

  /** * @covers ::getRuntimeContexts */
  public function testGetRuntimeMultipleContextsPerService() {
    $contexts = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1']);

    $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
    $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
    

  protected function getContextsForEntity(FieldableEntityInterface $entity) {
    $available_context_ids = array_keys($this->contextRepository()->getAvailableContexts());
    return [
      'view_mode' => new Context(ContextDefinition::create('string')$this->getMode()),
      'entity' => EntityContext::fromEntity($entity),
      'display' => EntityContext::fromEntity($this),
    ] + $this->contextRepository()->getRuntimeContexts($available_context_ids);
  }

  /** * {@inheritdoc} * * @todo Move this upstream in https://www.drupal.org/node/2939931. */
  public function label() {
    $bundle_info = \Drupal::service('entity_type.bundle.info')->getBundleInfo($this->getTargetEntityTypeId());
    $bundle_label = $bundle_info[$this->getTargetBundle()]['label'];
    $target_entity_type = $this->entityTypeManager()->getDefinition($this->getTargetEntityTypeId());
    
/** * {@inheritdoc} */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    return ['view_mode' => new Context(new ContextDefinition('string'), 'default')];
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    return $this->getRuntimeContexts([]);
  }

}
Home | Imprint | This part of the site doesn't use cookies.