WebformCardsManager.php in Webform 6.x
File
modules/webform_cards/src/WebformCardsManager.php
View source
<?php
namespace Drupal\webform_cards;
use Drupal\webform\Plugin\WebformElementManagerInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionConditionsValidatorInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform_cards\Plugin\WebformElement\WebformCard;
class WebformCardsManager implements WebformCardsManagerInterface {
protected $elementManager;
protected $conditionsValidator;
public function __construct(WebformElementManagerInterface $element_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator) {
$this->elementManager = $element_manager;
$this->conditionsValidator = $conditions_validator;
}
public function hasCards(WebformInterface $webform) {
return $this
->getNumberOfCards($webform) > 0 ? TRUE : FALSE;
}
public function getNumberOfCards(WebformInterface $webform) {
$elements = $webform
->getElementsDecoded();
$count = 0;
foreach ($elements as $element) {
if (is_array($element)) {
$element_plugin = $this->elementManager
->getElementInstance($element);
if ($element_plugin instanceof WebformCard) {
$count++;
}
}
}
return $count;
}
public function buildPages(WebformInterface $webform, $operation = 'default') {
$card_properties = [
'#title' => '#title',
'#states' => '#states',
];
$pages = [];
$elements = $webform
->getElementsInitialized();
if (is_array($elements) && !in_array($operation, [
'edit_all',
'api',
])) {
foreach ($elements as $key => $element) {
if (!isset($element['#type'])) {
continue;
}
$element_plugin = $this->elementManager
->getElementInstance($element, $webform);
if (!$element_plugin instanceof WebformCard) {
continue;
}
$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,
];
}
}
}
if ($webform
->getSetting('preview') !== DRUPAL_DISABLED) {
$pages[WebformInterface::PAGE_PREVIEW] = [
'#title' => $webform
->getSetting('preview_label', TRUE),
'#type' => 'page',
'#access' => TRUE,
];
}
if ($webform
->getSetting('wizard_confirmation')) {
$pages[WebformInterface::PAGE_CONFIRMATION] = [
'#title' => $webform
->getSetting('wizard_confirmation_label', TRUE),
'#type' => 'page',
'#access' => TRUE,
];
}
return $pages;
}
public function applyConditions(array $pages, WebformSubmissionInterface $webform_submission = NULL) {
if ($webform_submission && $webform_submission
->getWebform()
->getSetting('wizard_progress_states')) {
return $this->conditionsValidator
->buildPages($pages, $webform_submission);
}
else {
return $pages;
}
}
}