You are here

class WebformCardsManager in Webform 8.5

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

Manage webform cards.

Hierarchy

Expanded class hierarchy of WebformCardsManager

1 string reference to 'WebformCardsManager'
webform_cards.services.yml in modules/webform_cards/webform_cards.services.yml
modules/webform_cards/webform_cards.services.yml
1 service uses WebformCardsManager
webform_cards.manager in modules/webform_cards/webform_cards.services.yml
Drupal\webform_cards\WebformCardsManager

File

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

Namespace

Drupal\webform_cards
View source
class WebformCardsManager implements WebformCardsManagerInterface {

  /**
   * The webform element manager.
   *
   * @var \Drupal\webform\Plugin\WebformElementManagerInterface
   */
  protected $elementManager;

  /**
   * The webform submission (server-side) conditions (#states) validator.
   *
   * @var \Drupal\webform\WebformSubmissionConditionsValidator
   */
  protected $conditionsValidator;

  /**
   * Constructs a WebformCardsManager object.
   *
   * @param \Drupal\webform\Plugin\WebformElementManagerInterface $element_manager
   *   The webform element manager.
   * @param \Drupal\webform\WebformSubmissionConditionsValidatorInterface $conditions_validator
   *   The webform submission conditions (#states) validator.
   */
  public function __construct(WebformElementManagerInterface $element_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator) {
    $this->elementManager = $element_manager;
    $this->conditionsValidator = $conditions_validator;
  }

  /**
   * {@inheritdoc}
   */
  public function hasCards(WebformInterface $webform) {
    return $this
      ->getNumberOfCards($webform) > 0 ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * Update cards pages based on conditional logic (#states).
   *
   * @param array $pages
   *   An associative array of webform cards.
   * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
   *   A webform submission.
   *
   * @return array
   *   An associative array of webform cards with conditional logic applied.
   *
   * @see \Drupal\webform\Entity\Webform::getPages
   */
  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;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformCardsManager::$conditionsValidator protected property The webform submission (server-side) conditions (#states) validator.
WebformCardsManager::$elementManager protected property The webform element manager.
WebformCardsManager::applyConditions public function Update cards pages based on conditional logic (#states). Overrides WebformCardsManagerInterface::applyConditions
WebformCardsManager::buildPages public function Build webform's cards based on the current operation. Overrides WebformCardsManagerInterface::buildPages
WebformCardsManager::getNumberOfCards public function Counts the number of cards used in a webform. Overrides WebformCardsManagerInterface::getNumberOfCards
WebformCardsManager::hasCards public function Determine if a webform has cards. Overrides WebformCardsManagerInterface::hasCards
WebformCardsManager::__construct public function Constructs a WebformCardsManager object.