page_tester-1.0.x-dev/src/Plugin/Tests/TestsMenuLinks.php

src/Plugin/Tests/TestsMenuLinks.php
<?php

declare(strict_types=1);

namespace Drupal\page_tester\Plugin\Tests;

use Drupal\Core\Url;
use Drupal\page_tester\TestsPluginBase;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\menu_link_content\Entity\MenuLinkContent;

/**
 * Plugin implementation of the tests.
 *
 * @Tests(
 *   id = "tests_menu_links",
 *   label = @Translation("Tests Menu Links"),
 *   description = @Translation("Tests for Menu Links.")
 * )
 */
final class TestsMenuLinks extends TestsPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getTests($testId): array {
    $testOptions = [];
    $tests = \Drupal::entityTypeManager()->getStorage('page_tester_test')->load($testId);
    /** @var Drupal\Core\Entity\EntityTypeManagerInterface $tests */
    foreach ($tests->get('menu_links')->getValue() as $index => $mlid) {
      $menuLink = MenuLinkContent::load($mlid);
      $testOptions[$index] = $menuLink->toUrl()->toString();
    }
    return $testOptions;
  }

  /**
   * {@inheritdoc}
   */
  public function setTest($testId, $testNumber): mixed {
    $contentTypeTestItems = $this->getTests($testId);

    if (isset($contentTypeTestItems[$testNumber])) {
      $url = Url::fromUri('internal:' . $contentTypeTestItems[$testNumber]);

      // Create a RedirectResponse object with the URL.
      $response = new RedirectResponse($url->toString());

      // Prepare the response for Drupal's kernel.
      $request = \Drupal::request();
      $response->prepare($request);

      // Trigger kernel events for the redirect.
      \Drupal::service('kernel')->terminate($request, $response);

      // Send the redirect response.
      $response->send();
    }
    return NULL;
  }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc