You are here

public function WebformEditorialController::help in Webform 8.5

Returns webform help editorial.

Return value

array A renderable array containing webform help editorial.

1 string reference to 'WebformEditorialController::help'
webform_editorial.routing.yml in modules/webform_editorial/webform_editorial.routing.yml
modules/webform_editorial/webform_editorial.routing.yml

File

modules/webform_editorial/src/Controller/WebformEditorialController.php, line 142

Class

WebformEditorialController
Provides route responses for webform editorial.

Namespace

Drupal\webform_editorial\Controller

Code

public function help() {
  $help = $this->helpManager
    ->getHelp();
  $groups = $this->helpManager
    ->getGroup();
  foreach ($groups as $group_name => $group) {
    $groups[$group_name] = [];
  }
  foreach ($help as $name => $info) {
    $group_name = isset($info['group']) ? $info['group'] : (string) $this
      ->t('General');
    $groups[$group_name][$name] = $info;
  }
  $groups = array_filter($groups);
  $build = [];
  $build['title'] = [
    '#prefix' => '<h1>',
    '#suffix' => '</h1>',
    '#markup' => $this
      ->t('Webform: Help editorial'),
  ];
  $group_index = 1;
  foreach ($groups as $group_name => $help) {

    // Header.
    $header = [
      [
        'data' => $this
          ->t('Name'),
        'width' => '20%',
      ],
      [
        'data' => $this
          ->t('Title'),
        'width' => '20%',
      ],
      [
        'data' => $this
          ->t('Content'),
        'width' => '50%',
      ],
      [
        'data' => $this
          ->t('Links'),
        'width' => '10%',
      ],
    ];

    // Rows.
    $rows = [];
    foreach ($help as $name => $info) {

      // Paths.
      $paths = [];
      if (!empty($info['paths'])) {
        $paths = array_merge($paths, $info['paths']);
      }
      if (!empty($info['routes'])) {
        $paths = array_merge($paths, $this->database
          ->query("SELECT path FROM {router} WHERE name IN (:name[])", [
          ':name[]' => $info['routes'],
        ])
          ->fetchCol() ?: []);
      }
      asort($paths);
      foreach ($paths as $index => $path) {
        $path = str_replace('/admin/structure/webform/', '../', $path);
        $path = str_replace('/admin/structure/', '../', $path);
        $path = preg_replace('/\\{[^}]+\\}/', '*', $path);
        $paths[$index] = $path;
      }
      $name = '<b>' . $name . '</b><br/><small><small><em>' . implode('<br />', $paths) . '</em></small></small>';

      // Links.
      $links = [];
      if (!empty($info['video_id'])) {
        $video = $this->helpManager
          ->getVideo($info['video_id']);
        $links[] = Link::fromTextAndUrl('Video', Url::fromUri('https://www.youtube.com/watch', [
          'query' => [
            'v' => $video['youtube_id'],
          ],
        ]))
          ->toString();
      }
      if (is_array($info['content'])) {
        $info['content'] = $this->renderer
          ->renderPlain($info['content']);
      }
      $rows[] = [
        'data' => [
          [
            'data' => $name,
          ],
          [
            'data' => $info['title'],
          ],
          [
            'data' => $info['content'],
          ],
          [
            'data' => implode(' | ', $links),
          ],
        ],
      ];
    }
    $title = ($this->helpManager
      ->getGroup($group_name) ?: $group_name) . ' [' . str_pad($group_index++, 2, '0', STR_PAD_LEFT) . ' - ' . $group_name . ']';
    $build[$group_name] = $this
      ->buildTable($title, $header, $rows, 'h2');
  }

  // Add videos.
  $presentations = $this->helpManager
    ->getVideo();
  foreach ($presentations as $presentation_name => $presentation) {
    $build[$presentation_name]['description']['title'] = [
      '#markup' => $presentation['title'],
      '#prefix' => '<strong>',
      '#suffix' => '</strong><br/>',
    ];
    $build[$presentation_name]['description']['content'] = [
      '#markup' => $presentation['content'],
      '#suffix' => '<br/><br/>',
    ];
  }
  return $this
    ->response($build);
}