loadEmpty example



  /** * Alters existing routes for a specific collection. * * @param \Drupal\Core\Routing\RouteBuildEvent $event * The route build event. */
  public function onAlterRoutes(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
    foreach ($this->sectionStorageManager->getDefinitions() as $plugin_id => $definition) {
      $this->sectionStorageManager->loadEmpty($plugin_id)->buildRoutes($collection);
    }
  }

  /** * {@inheritdoc} */
  public static function getSubscribedEvents(): array {
    // Run after \Drupal\field_ui\Routing\RouteSubscriber.     $events[RoutingEvents::ALTER] = ['onAlterRoutes', -110];
    return $events;
  }

}
$section_storage_manager = $this->prophesize(SectionStorageManagerInterface::class);
    $converter = new LayoutSectionStorageParamConverter($section_storage_manager->reveal());

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

    $value = 'some_value';
    $definition = ['layout_builder_tempstore' => TRUE];
    $name = 'the_parameter_name';
    $defaults = ['section_storage_type' => 'my_type'];

    $section_storage_manager->hasDefinition('my_type')->willReturn(TRUE);
    $section_storage_manager->loadEmpty('my_type')->willReturn($section_storage->reveal());
    $section_storage->deriveContextsFromRoute($value$definition$name$defaults)->willReturn([]);
    $section_storage_manager->load('my_type', [])->willReturn($section_storage->reveal());

    $result = $converter->convert($value$definition$name$defaults);
    $this->assertSame($section_storage->reveal()$result);
  }

  /** * @covers ::convert */
  public function testConvertNoType() {
    
/** * {@inheritdoc} */
  public function convert($value$definition$name, array $defaults) {
    // If no section storage type is specified or if it is invalid, return.     if (!isset($defaults['section_storage_type']) || !$this->sectionStorageManager->hasDefinition($defaults['section_storage_type'])) {
      return NULL;
    }

    $type = $defaults['section_storage_type'];
    // Load an empty instance and derive the available contexts.     $contexts = $this->sectionStorageManager->loadEmpty($type)->deriveContextsFromRoute($value$definition$name$defaults);
    // Attempt to load a full instance based on the context.     return $this->sectionStorageManager->load($type$contexts);
  }

  /** * {@inheritdoc} */
  public function applies($definition$name, Route $route) {
    return !empty($definition['layout_builder_section_storage']) || !empty($definition['layout_builder_tempstore']);
  }

}
return $definition->getWeight();
    }$definitions);
    $ids = array_keys($definitions);
    array_multisort($weights$ids$definitions);
    return $definitions;
  }

  /** * {@inheritdoc} */
  public function load($type, array $contexts = []) {
    $plugin = $this->loadEmpty($type);
    try {
      $this->contextHandler->applyContextMapping($plugin$contexts);
    }
    catch (ContextException $e) {
      return NULL;
    }
    return $plugin;
  }

  /** * {@inheritdoc} */
return new static(
      $container->get('entity_type.manager'),
      $container->get('plugin.manager.layout_builder.section_storage')
    );
  }

  /** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->sectionStorageManager->getDefinitions() as $plugin_id => $definition) {
      $section_storage = $this->sectionStorageManager->loadEmpty($plugin_id);
      if ($section_storage instanceof SectionStorageLocalTaskProviderInterface) {
        $this->derivatives += $section_storage->buildLocalTasks($base_plugin_definition);
      }
    }
    return $this->derivatives;
  }

}
$this->plugin = $this->prophesize(SectionStorageInterface::class);
    $this->factory = $this->prophesize(FactoryInterface::class);
    $this->factory->createInstance('the_plugin_id', [])->willReturn($this->plugin->reveal());
    $reflection_property = new \ReflectionProperty($this->manager, 'factory');
    $reflection_property->setValue($this->manager, $this->factory->reveal());
  }

  /** * @covers ::loadEmpty */
  public function testLoadEmpty() {
    $result = $this->manager->loadEmpty('the_plugin_id');
    $this->assertInstanceOf(SectionStorageInterface::class$result);
    $this->assertSame($this->plugin->reveal()$result);
  }

  /** * @covers ::load */
  public function testLoad() {
    $contexts = [
      'the_context' => $this->prophesize(ContextInterface::class)->reveal(),
    ];

    
$collection->add('test_route_shared', new Route('/test/path/shared1'));
      $collection->add('test_route1', new Route('/test/path1'));
    });
    $section_storage_second = $this->prophesize(SectionStorageInterface::class);
    $section_storage_second->buildRoutes(Argument::type(RouteCollection::class))->shouldBeCalled()->will(function D$args) {
      /** @var \Symfony\Component\Routing\RouteCollection $collection */
      $collection = $args[0];
      $collection->add('test_route_shared', new Route('/test/path/shared2'));
      $collection->add('test_route2', new Route('/test/path2'));
    });

    $this->sectionStorageManager->loadEmpty('first')->willReturn($section_storage_first->reveal());
    $this->sectionStorageManager->loadEmpty('second')->willReturn($section_storage_second->reveal());
    $definitions['first'] = new SectionStorageDefinition();
    $definitions['second'] = new SectionStorageDefinition();
    $this->sectionStorageManager->getDefinitions()->willReturn($definitions);

    $collection = new RouteCollection();
    $event = new RouteBuildEvent($collection);
    $this->routeBuilder->onAlterRoutes($event);
    $this->assertEquals($expected$collection->all());
    $this->assertSame(array_keys($expected)array_keys($collection->all()));
  }

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