You are here

public function CookiesConfigService::getRenderedCookiesDocs in COOKiES Consent Management 1.0.x

Renders the documentation listing.

Return value

array Rendered cookie documentation listing.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Services/CookiesConfigService.php, line 399

Class

CookiesConfigService
Services to handle module config and method for a rendered documentation.

Namespace

Drupal\cookies\Services

Code

public function getRenderedCookiesDocs() : array {
  $listing = [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'cookies-docs',
    ],
    '#cache' => [
      'max-age' => Cache::PERMANENT,
      'contexts' => [
        'languages',
        'user.roles',
      ],
      'tags' => [
        'config:cookies.config',
        'config:cookies.texts',
        'config:cookies.cookies_service',
        'config:cookies.cookies_service_group',
      ],
    ],
  ];
  $config = $this
    ->getCookiesConfig();
  $groups = $config['services'] ?? [];
  foreach ($groups as $group_name => $group) {

    /** @var \Drupal\cookies\Entity\CookiesServiceGroup $group_definition */
    $group_definition = $this
      ->getGroup($group_name);
    $rendered_group = [
      '#theme' => 'cookies_docs_group',
      '#attributes' => [
        'id' => $group_definition
          ->id(),
      ],
      '#label' => $group_definition
        ->label(),
      '#items' => [],
    ];
    foreach ($group['services'] as $service) {
      $add_service = FALSE;
      $rendered_service = [
        '#theme' => 'cookies_docs_service',
        '#attributes' => [
          'id' => $service['key'],
        ],
        '#label' => $service['name'],
      ];

      // Add link for official website of the service provider.
      if ($service['uri']) {
        $ext_url = UrlHelper::isValid($service['uri'], TRUE) ? $service['uri'] : FALSE;
        if ($ext_url) {
          $add_service = TRUE;
          $rendered_service['#external_link_url'] = $ext_url;
          $rendered_service['#external_link_text'] = new TranslatableMarkup('Official %name website.', [
            '%name' => $service['name'],
          ]);
        }
      }

      // Add service info.
      if ($service['info']) {
        $add_service = TRUE;
        $rendered_service['#content'] = [
          '#markup' => $service['info']['value'],
        ];
      }

      // Add edit link for user with permission to edit cookies configuration.
      if ($this->currentUser
        ->hasPermission('configure cookies widget')) {
        $add_service = TRUE;
        $rendered_service['#edit_link_url'] = Url::fromRoute('entity.cookies_service.edit_form', [
          'cookies_service' => $service['key'],
        ])
          ->toString();
        $rendered_service['#edit_link_text'] = new TranslatableMarkup('Edit cookie information for %name here.', [
          '%name' => $service['name'],
        ]);
      }
      if ($add_service) {
        $rendered_group['#items'][] = $rendered_service;
      }
    }
    $listing[$group_name] = $rendered_group;
  }
  return $listing;
}