getAvailableContexts example

return NULL;
      }
      return $entity;
    }

    // Do not inject the context repository as it is not an actual dependency:     // it will be removed once both the TODOs below are fixed.     /** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $contexts_repository */
    $contexts_repository = \Drupal::service('context.repository');
    // @todo Consider deprecating the legacy context operation altogether in     // https://www.drupal.org/node/3031124.     $contexts = $contexts_repository->getAvailableContexts();
    $contexts[EntityRepositoryInterface::CONTEXT_ID_LEGACY_CONTEXT_OPERATION] =
      new Context(new ContextDefinition('string'), 'entity_upcast');
    // @todo At the moment we do not need the current user context, which is     // triggering some test failures. We can remove these lines once     // https://www.drupal.org/node/2934192 is fixed.     $context_id = '@user.current_user_context:current_user';
    if (isset($contexts[$context_id])) {
      $account = $contexts[$context_id]->getContextValue();
      unset($account->_skipProtectedUserFieldConstraint);
      unset($contexts[$context_id]);
    }
    
/** * Gets the available contexts for a given entity. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity. * * @return \Drupal\Core\Plugin\Context\ContextInterface[] * An array of context objects for a given entity. */
  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 testGetAvailableContexts() {
    $context_repository = $this->container->get('context.repository');

    // Test an authenticated account.     $authenticated = User::create([
      'name' => $this->randomMachineName(),
    ]);
    $authenticated->save();
    $authenticated = User::load($authenticated->id());
    $this->container->get('current_user')->setAccount($authenticated);

    $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
    $this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
    $this->assertTrue($contexts['@user.current_user_context:current_user']->hasContextValue());
    $this->assertNotNull($contexts['@user.current_user_context:current_user']->getContextValue());

    // Test an anonymous account.     $anonymous = $this->prophesize(AccountInterface::class);
    $anonymous->id()->willReturn(0);
    $this->container->get('current_user')->setAccount($anonymous->reveal());

    $contexts = $context_repository->getAvailableContexts();
    

  }

  /** * {@inheritdoc} */
  public function form(array $form, FormStateInterface $form_state) {
    $entity = $this->entity;

    // Store the gathered contexts in the form state for other objects to use     // during form building.     $form_state->setTemporaryValue('gathered_contexts', $this->contextRepository->getAvailableContexts());

    $form['#tree'] = TRUE;
    $form['settings'] = [];
    $subform_state = SubformState::createForSubform($form['settings']$form$form_state);
    $form['settings'] = $this->getPluginForm($entity->getPlugin())->buildConfigurationForm($form['settings']$subform_state);
    $form['visibility'] = $this->buildVisibilityInterface([]$form_state);

    // If creating a new block, calculate a safe default machine name.     $form['id'] = [
      '#type' => 'machine_name',
      '#maxlength' => 64,
      
$this->installEntitySchema('user');
    $this->installEntitySchema('taxonomy_term');
  }

  /** * @covers ::getAvailableContexts */
  public function testGetAvailableContexts() {
    $context_repository = $this->container->get('context.repository');

    // Test taxonomy_term.taxonomy_term_route_context:taxonomy_term exists.     $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@taxonomy_term.taxonomy_term_route_context:taxonomy_term', $contexts);
    $this->assertSame('entity:taxonomy_term', $contexts['@taxonomy_term.taxonomy_term_route_context:taxonomy_term']->getContextDefinition()
      ->getDataType());
  }

  /** * @covers ::getRuntimeContexts */
  public function testGetRuntimeContexts() {
    // Create term.     $vocabulary = $this->createVocabulary();
    


    return $contexts;
  }

  /** * {@inheritdoc} */
  public function getAvailableContexts() {
    $contexts = [];
    foreach ($this->contextProviderServiceIDs as $service_id) {
      $contexts_by_service = $this->container->get($service_id)->getAvailableContexts();
      foreach ($contexts_by_service as $unqualified_context_id => $context) {
        $context_id = '@' . $service_id . ':' . $unqualified_context_id;
        $contexts[$context_id] = $context;
      }
    }

    return $contexts;
  }

}
public function getActive($entity_type_id$entity_id, array $contexts = NULL) {
    return current($this->getActiveMultiple($entity_type_id[$entity_id]$contexts)) ?: NULL;
  }

  /** * {@inheritdoc} */
  public function getActiveMultiple($entity_type_id, array $entity_ids, array $contexts = NULL) {
    $active = [];

    if (!isset($contexts)) {
      $contexts = $this->contextRepository->getAvailableContexts();
    }

    // @todo Consider implementing a more performant version of this logic fully     // supporting multiple entities in https://www.drupal.org/node/3031082.     $langcode = $this->languageManager->isMultilingual()
      ? $this->getContentLanguageFromContexts($contexts)
      : $this->languageManager->getDefaultLanguage()->getId();

    $entities = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->loadMultiple($entity_ids);

    
/** * Returns all populated contexts, both global and section-storage-specific. * * @param \Drupal\layout_builder\SectionStorageInterface $section_storage * The section storage. * * @return \Drupal\Core\Plugin\Context\ContextInterface[] * The array of context objects. */
  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;
  }

}
$lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
  }

  /** * @covers ::getAvailableContexts */
  public function testGetAvailableContexts() {
    $contexts0 = $this->setupContextAndProvider('test_provider0', ['test0_context0', 'test0_context1']);
    $contexts1 = $this->setupContextAndProvider('test_provider1', ['test1_context0', 'test1_context1']);

    $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider0', 'test_provider1']);
    $contexts = $lazy_context_repository->getAvailableContexts();

    $this->assertEquals([
      '@test_provider0:test0_context0' => $contexts0[0],
      '@test_provider0:test0_context1' => $contexts0[1],
      '@test_provider1:test1_context0' => $contexts1[0],
      '@test_provider1:test1_context1' => $contexts1[1],
    ]$contexts);

  }

  /** * Sets up contexts and context providers. * * @param string $service_id * The service ID of the service provider. * @param string[] $unqualified_context_ids * An array of context slot names. * @param string[] $expected_unqualified_context_ids * The expected unqualified context IDs passed to getRuntimeContexts. * * @return array * An array of set up contexts. */
$headers = [
      ['data' => $this->t('Block')],
      ['data' => $this->t('Category')],
      ['data' => $this->t('Operations')],
    ];

    $region = $request->query->get('region');
    $weight = $request->query->get('weight');

    // Only add blocks which work without any available context.     $definitions = $this->blockManager->getFilteredDefinitions('block_ui', $this->contextRepository->getAvailableContexts()[
      'theme' => $theme,
      'region' => $region,
    ]);
    // Order by category, and then by admin label.     $definitions = $this->blockManager->getSortedDefinitions($definitions);
    // Filter out definitions that are not intended to be placed by the UI.     $definitions = array_filter($definitionsfunction Darray $definition) {
      return empty($definition['_block_ui_hidden']);
    });

    $rows = [];
    
Home | Imprint | This part of the site doesn't use cookies.