findByContext example


  protected function buildSections(FieldableEntityInterface $entity) {
    $contexts = $this->getContextsForEntity($entity);
    // @todo Remove in https://www.drupal.org/project/drupal/issues/3018782.     $label = new TranslatableMarkup('@entity being viewed', [
      '@entity' => $entity->getEntityType()->getSingularLabel(),
    ]);
    $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity$label);

    $cacheability = new CacheableMetadata();
    $storage = $this->sectionStorageManager()->findByContext($contexts$cacheability);

    $build = [];
    if ($storage) {
      foreach ($storage->getSections() as $delta => $section) {
        $build[$delta] = $section->toRenderArray($contexts);
      }
    }
    // The render array is built based on decisions made by @SectionStorage     // plugins and therefore it needs to depend on the accumulated     // cacheability of those decisions.     $cacheability->applyTo($build);
    

    else {
      $contexts['entity'] = EntityContext::fromEntity($entity);
      if ($entity instanceof FieldableEntityInterface) {
        $display = EntityViewDisplay::collectRenderDisplay($entity$view_mode);
        if ($display instanceof LayoutEntityDisplayInterface) {
          $contexts['display'] = EntityContext::fromEntity($display);
        }
        $contexts['view_mode'] = new Context(new ContextDefinition('string')$view_mode);
      }
    }
    return $this->sectionStorageManager()->findByContext($contextsnew CacheableMetadata());
  }

  /** * Determines if the original entity used the default section storage. * * This method can be used during the entity save process to determine whether * $entity->original is set and used the default section storage plugin as * determined by ::getSectionStorageForEntity(). * * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * * @return bool * TRUE if the original entity used the default storage. */
return $data;
  }

  /** * @covers ::getSectionStorageForEntity * * @dataProvider providerTestGetSectionStorageForEntity */
  public function testGetSectionStorageForEntity($entity_type_id$values$expected_context_keys) {
    $section_storage_manager = $this->prophesize(SectionStorageManagerInterface::class);
    $section_storage_manager->load('')->willReturn(NULL);
    $section_storage_manager->findByContext(Argument::cetera())->will(function D$arguments) {
      return $arguments[0];
    });
    $this->container->set('plugin.manager.layout_builder.section_storage', $section_storage_manager->reveal());
    $entity = $this->container->get('entity_type.manager')->getStorage($entity_type_id)->create($values);
    $entity->save();
    $class = new TestLayoutEntityHelperTrait();
    $result = $class->getSectionStorageForEntity($entity);
    $this->assertEquals($expected_context_keysarray_keys($result));
    if ($entity instanceof EntityViewDisplayInterface) {
      $this->assertEquals(EntityContext::fromEntity($entity)$result['display']);
    }
    
// 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);
    }
    else {
      $this->assertNull($result);
    }
  }

  /** * Provides test data for ::testFindByContext(). */
  
Home | Imprint | This part of the site doesn't use cookies.