whitelabel-8.x-2.x-dev/tests/modules/whitelabel_test/src/Plugin/WhiteLabelNegotiation/WhiteLabelNegotiationFixed.php

tests/modules/whitelabel_test/src/Plugin/WhiteLabelNegotiation/WhiteLabelNegotiationFixed.php
<?php

namespace Drupal\whitelabel_test\Plugin\WhiteLabelNegotiation;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\whitelabel\Entity\WhiteLabel;
use Drupal\whitelabel\WhiteLabelNegotiationMethodBase;
use Drupal\whitelabel\WhiteLabelNegotiationMethodInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class for identifying white labels via a fixed value.
 *
 * @WhiteLabelNegotiation(
 *   id = \Drupal\whitelabel_test\Plugin\WhiteLabelNegotiation\WhiteLabelNegotiationFixed::METHOD_ID,
 *   label = @Translation("Fixed"),
 *   description = @Translation("Uses a fixed configurable white label."),
 *   weight = -10,
 * )
 */
class WhiteLabelNegotiationFixed extends WhiteLabelNegotiationMethodBase implements WhiteLabelNegotiationMethodInterface {

  use StringTranslationTrait;

  /**
   * The white label negotiation method id.
   */
  const METHOD_ID = 'fixed';

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'whitelabel_id' => NULL,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getWhiteLabel(Request $request = NULL) {
    if ($whitelabel_id = $this->configuration['whitelabel_id']) {
      if ($whitelabel = WhiteLabel::load($whitelabel_id)) {
        // Return the white label if there is one.
        return $whitelabel;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['whitelabel_id'] = [
      '#type' => 'number',
      '#title' => t('The white label ID to use.'),
      '#default_value' => $this->configuration['whitelabel_id'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $wid = $form_state->getValue('whitelabel_id');
    $whitelabel = \Drupal::entityQuery('whitelabel')->condition('wid', $wid)->execute();
    if (empty($whitelabel)) {
      $form_state->setErrorByName('whitelabel_id', t('The provided white label does not exist.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->configuration['whitelabel_id'] = $form_state->getValue('whitelabel_id');
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc