You are here

class AgreeTerms in Commerce Agree Terms 8

Provides the completion message pane.

Plugin annotation


@CommerceCheckoutPane(
  id = "agree_terms",
  label = @Translation("Agree to the terms and conditions"),
  default_step = "review",
)

Hierarchy

Expanded class hierarchy of AgreeTerms

File

src/Plugin/Commerce/CheckoutPane/AgreeTerms.php, line 20

Namespace

Drupal\commerce_agree_terms\Plugin\Commerce\CheckoutPane
View source
class AgreeTerms extends CheckoutPaneBase implements CheckoutPaneInterface {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'nid' => NULL,
      'link_text' => 'Terms and Conditions',
      'prefix_text' => 'I agree with the %terms',
      'invalid_text' => 'You must agree with the %terms before continuing',
      'new_window' => 1,
    ] + parent::defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationSummary() {
    $prefix = $this->configuration['prefix_text'];
    $link_text = $this->configuration['link_text'];
    $invalid_text = $this->configuration['invalid_text'];
    $new_window = $this->configuration['new_window'];
    $nid = $this->configuration['nid'];
    $summary = '';
    if (!empty($prefix)) {
      $summary = $this
        ->t('Prefix text: @text', [
        '@text' => $prefix,
      ]) . '<br/>';
    }
    if (!empty($link_text)) {
      $summary .= $this
        ->t('Link text: @text', [
        '@text' => $link_text,
      ]) . '<br/>';
    }
    if (!empty($invalid_text)) {
      $summary .= $this
        ->t('Error text: @text', [
        '@text' => $invalid_text,
      ]) . '<br/>';
    }
    if (!empty($window_target)) {
      $window_text = $new_window === 1 ? $this
        ->t('New window') : $this
        ->t('Same window');
      $summary .= $this
        ->t('Window opens in: @opens', [
        '@text' => $window_text,
      ]) . '<br/>';
    }
    if (!empty($nid)) {
      $node = Node::load($nid);
      if ($node) {
        $summary .= $this
          ->t('Terms page: @title', [
          '@title' => $node
            ->getTitle(),
        ]);
      }
    }
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['prefix_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Prefix text'),
      '#default_value' => $this->configuration['prefix_text'],
    ];
    $form['link_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Link text'),
      '#default_value' => $this->configuration['link_text'],
      '#required' => TRUE,
    ];
    $form['invalid_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Invalid text'),
      '#default_value' => $this->configuration['invalid_text'],
    ];
    $form['new_window'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Open window link in new window'),
      '#default_value' => $this->configuration['new_window'],
    ];
    if ($this->configuration['nid']) {
      $node = Node::load($this->configuration['nid']);
    }
    else {
      $node = NULL;
    }
    $form['nid'] = [
      '#type' => 'entity_autocomplete',
      '#target_type' => 'node',
      '#default_value' => $node,
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    if (!$form_state
      ->getErrors()) {
      $values = $form_state
        ->getValue($form['#parents']);
      $this->configuration['prefix_text'] = $values['prefix_text'];
      $this->configuration['link_text'] = $values['link_text'];
      $this->configuration['invalid_text'] = $values['invalid_text'];
      $this->configuration['new_window'] = $values['new_window'];
      $this->configuration['nid'] = $values['nid'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $prefix_text = $this->configuration['prefix_text'];
    $link_text = $this->configuration['link_text'];
    $nid = $this->configuration['nid'];
    if ($nid) {
      $node = Node::load($nid);
      $attributes = [];
      if ($this->configuration['new_window']) {
        $attributes = [
          'attributes' => [
            'target' => '_blank',
          ],
        ];
      }
      $link = Link::createFromRoute($this
        ->t($link_text), 'entity.node.canonical', [
        'node' => $nid,
      ], $attributes)
        ->toString();
      if ($prefix_text) {
        $pane_form['terms_and_conditions'] = [
          '#type' => 'checkbox',
          '#default_value' => FALSE,
          '#title' => $this
            ->t($prefix_text, [
            '%terms' => $link,
          ]),
          '#required' => TRUE,
          '#weight' => $this
            ->getWeight(),
        ];
      }
      else {
        $pane_form['terms_and_conditions'] = [
          '#type' => 'checkbox',
          '#default_value' => FALSE,
          '#title' => $link,
          '#required' => TRUE,
          '#weight' => $this
            ->getWeight(),
        ];
      }
    }
    return $pane_form;
  }

  /**
   * {@inheritdoc}
   */
  public function validatePaneForm(array &$pane_form, FormStateInterface $form_state, array &$complete_form) {
    $values = $form_state
      ->getValue($pane_form['#parents']);
    if (!$values['terms_and_conditions']) {
      $form_state
        ->setError($pane_form, $this->configuration['invalid_text']);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AgreeTerms::buildConfigurationForm public function Form constructor. Overrides CheckoutPaneBase::buildConfigurationForm
AgreeTerms::buildConfigurationSummary public function Builds a summary of the pane configuration. Overrides CheckoutPaneBase::buildConfigurationSummary
AgreeTerms::buildPaneForm public function Builds the pane form. Overrides CheckoutPaneInterface::buildPaneForm
AgreeTerms::defaultConfiguration public function Gets default configuration for this plugin. Overrides CheckoutPaneBase::defaultConfiguration
AgreeTerms::submitConfigurationForm public function Form submission handler. Overrides CheckoutPaneBase::submitConfigurationForm
AgreeTerms::validatePaneForm public function Validates the pane form. Overrides CheckoutPaneBase::validatePaneForm
CheckoutPaneBase::$checkoutFlow protected property The parent checkout flow.
CheckoutPaneBase::$entityTypeManager protected property The entity type manager.
CheckoutPaneBase::$order protected property The current order.
CheckoutPaneBase::buildPaneSummary public function Builds a summary of the pane values. Overrides CheckoutPaneInterface::buildPaneSummary 3
CheckoutPaneBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
CheckoutPaneBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 7
CheckoutPaneBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CheckoutPaneBase::getDisplayLabel public function Gets the pane display label. Overrides CheckoutPaneInterface::getDisplayLabel
CheckoutPaneBase::getId public function Gets the pane ID. Overrides CheckoutPaneInterface::getId
CheckoutPaneBase::getLabel public function Gets the pane label. Overrides CheckoutPaneInterface::getLabel
CheckoutPaneBase::getStepId public function Gets the pane step ID. Overrides CheckoutPaneInterface::getStepId
CheckoutPaneBase::getWeight public function Gets the pane weight. Overrides CheckoutPaneInterface::getWeight
CheckoutPaneBase::getWrapperElement public function Gets the pane wrapper element. Overrides CheckoutPaneInterface::getWrapperElement
CheckoutPaneBase::isVisible public function Determines whether the pane is visible. Overrides CheckoutPaneInterface::isVisible 4
CheckoutPaneBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CheckoutPaneBase::setOrder public function Sets the current order. Overrides CheckoutPaneInterface::setOrder
CheckoutPaneBase::setStepId public function Sets the pane step ID. Overrides CheckoutPaneInterface::setStepId
CheckoutPaneBase::setWeight public function Sets the pane weight. Overrides CheckoutPaneInterface::setWeight
CheckoutPaneBase::submitPaneForm public function Handles the submission of an pane form. Overrides CheckoutPaneInterface::submitPaneForm 7
CheckoutPaneBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
CheckoutPaneBase::__construct public function Constructs a new CheckoutPaneBase object. Overrides PluginBase::__construct 6
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.