You are here

public function WebformCardsManager::buildPages in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_cards/src/WebformCardsManager.php \Drupal\webform_cards\WebformCardsManager::buildPages()

Build webform's cards based on the current operation.

Parameters

\Drupal\webform\WebformInterface $webform: A webform.

string $operation: The webform submission operation. Usually 'default', 'add', 'edit', 'edit_all', 'api', or 'test'.

Return value

array An associative array of webform cards.

Overrides WebformCardsManagerInterface::buildPages

See also

\Drupal\webform\Entity\Webform::buildPages

File

modules/webform_cards/src/WebformCardsManager.php, line 70

Class

WebformCardsManager
Manage webform cards.

Namespace

Drupal\webform_cards

Code

public function buildPages(WebformInterface $webform, $operation = 'default') {
  $card_properties = [
    '#title' => '#title',
    '#states' => '#states',
  ];
  $pages = [];

  // Add webform cards.
  $elements = $webform
    ->getElementsInitialized();
  if (is_array($elements) && !in_array($operation, [
    'edit_all',
    'api',
  ])) {
    foreach ($elements as $key => $element) {
      if (!isset($element['#type'])) {
        continue;
      }

      /** @var \Drupal\webform\Plugin\WebformElementInterface $element_plugin */
      $element_plugin = $this->elementManager
        ->getElementInstance($element, $webform);
      if (!$element_plugin instanceof WebformCard) {
        continue;
      }

      // Check element access rules and only include pages that are visible
      // to the current user.
      $access_operation = in_array($operation, [
        'default',
        'add',
      ]) ? 'create' : 'update';
      if ($element_plugin
        ->checkAccessRules($access_operation, $element)) {
        $pages[$key] = array_intersect_key($element, $card_properties) + [
          '#type' => 'card',
          '#access' => TRUE,
        ];
      }
    }
  }

  // Add preview.
  if ($webform
    ->getSetting('preview') !== DRUPAL_DISABLED) {
    $pages[WebformInterface::PAGE_PREVIEW] = [
      '#title' => $webform
        ->getSetting('preview_label', TRUE),
      '#type' => 'page',
      '#access' => TRUE,
    ];
  }

  // Add confirmation.
  if ($webform
    ->getSetting('wizard_confirmation')) {
    $pages[WebformInterface::PAGE_CONFIRMATION] = [
      '#title' => $webform
        ->getSetting('wizard_confirmation_label', TRUE),
      '#type' => 'page',
      '#access' => TRUE,
    ];
  }
  return $pages;
}