getRelated example


    protected function prepareRelatedAssociatedData($data, ProductModel $article)
    {
        if (!isset($data['related'])) {
            return $data;
        }

        $related = $this->checkDataReplacement($article->getRelated()$data, 'related', true);

        foreach ($data['related'] as $relatedData) {
            if (empty($relatedData['number']) && empty($relatedData['id'])) {
                continue;
            }

            $relatedProduct = null;
            if (!empty($relatedData['number'])) {
                $productId = $this->getManager()->getConnection()->fetchOne(
                    'SELECT articleID FROM s_articles_details WHERE ordernumber = :number',
                    [':number' => $relatedData['number']]
                );
/** @var \Drupal\help_topics\HelpTopicPluginInterface $help_topic */
    $help_topic = $this->helpTopicPluginManager->createInstance($id);

    $build['#body'] = $help_topic->getBody();

    $this->renderer->addCacheableDependency($build$help_topic);

    // Build the related topics section, starting with the list this topic     // says are related.     $links = [];

    $related = $help_topic->getRelated();
    foreach ($related as $other_id) {
      if ($other_id !== $id) {
        /** @var \Drupal\help_topics\HelpTopicPluginInterface $topic */
        $topic = $this->helpTopicPluginManager->createInstance($other_id);
        $links[$other_id] = [
          'title' => $topic->getLabel(),
          'url' => Url::fromRoute('help.help_topic', ['id' => $other_id]),
        ];
        $this->renderer->addCacheableDependency($build$topic);
      }
    }

    
$related = [];
        foreach ($data['related'] ?? [] as $relatedData) {
            if (empty($relatedData['id'])) {
                continue;
            }
            $relatedProduct = $this->getRepository()->find($relatedData['id']);
            if (!$relatedProduct instanceof Product) {
                throw new ModelNotFoundException(Product::class$relatedData['id']);
            }

            // if the user select the cross             if ($relatedData['cross'] && !$relatedProduct->getRelated()->contains($article)) {
                $relatedProduct->getRelated()->add($article);
                $this->get('models')->persist($relatedProduct);
            }
            $related[] = $relatedProduct;
        }
        $data['related'] = $related;

        return $data;
    }

    /** * This function loads the related product stream models for the passed ids in the "streams" parameter. * * @param array<string, mixed> $data * * @return array<string, mixed> */
/** * @covers ::getProvider * @covers ::isTopLevel * @covers ::getRelated */
  public function testDefinition() {
    $this->assertEquals($this->helpTopic->getProvider(),
      self::PLUGIN_INFORMATION['provider']);
    $this->assertEquals($this->helpTopic->isTopLevel(),
      self::PLUGIN_INFORMATION['top_level']);
    $this->assertEquals($this->helpTopic->getRelated(),
      self::PLUGIN_INFORMATION['related']);
  }

  /** * @covers ::getCacheContexts * @covers ::getCacheTags * @covers ::getCacheMaxAge */
  public function testCacheInfo() {
    $this->assertEquals([]$this->helpTopic->getCacheContexts());
    $this->assertEquals(['core.extension']$this->helpTopic->getCacheTags());
    
Home | Imprint | This part of the site doesn't use cookies.