You are here

class SimplePopupBlocksEditForm in Simple Popup Blocks 8.2

Same name and namespace in other branches
  1. 8 src/Form/SimplePopupBlocksEditForm.php \Drupal\simple_popup_blocks\Form\SimplePopupBlocksEditForm

Form to add a database entry, with all the interesting fields.

Hierarchy

Expanded class hierarchy of SimplePopupBlocksEditForm

1 string reference to 'SimplePopupBlocksEditForm'
simple_popup_blocks.routing.yml in ./simple_popup_blocks.routing.yml
simple_popup_blocks.routing.yml

File

src/Form/SimplePopupBlocksEditForm.php, line 12

Namespace

Drupal\simple_popup_blocks\Form
View source
class SimplePopupBlocksEditForm extends SimplePopupBlocksAddForm {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'simple_popup_blocks_edit_form';
  }
  protected function getEditableConfigNames() {
    return [
      'simple_popup_blocks_add_form.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $uid = NULL) {
    $configFactory = \Drupal::service('config.factory');
    $data = $configFactory
      ->get('simple_popup_blocks.popup_' . $uid);
    $form = parent::buildForm($form, $form_state);
    $form['status'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable this as popup.'),
      '#default_value' => $data
        ->get('status'),
      '#weight' => -99,
    ];
    $visit_counts = unserialize($data
      ->get('visit_counts'));
    $identifier = $data
      ->get('identifier');
    $form['uid']['#default_value'] = $data
      ->get('uid');
    $form['uid']['#disabled'] = TRUE;
    $form['type']['#default_value'] = $data
      ->get('type');
    $form['block_list']['#default_value'] = $identifier;
    $form['custom_css']['#default_value'] = $identifier;
    $form['css_selector']['#default_value'] = $data
      ->get('css_selector');
    $form['layout']['#default_value'] = $data
      ->get('layout');
    $form['minimize']['#default_value'] = $data
      ->get('minimize');
    $form['close']['#default_value'] = $data
      ->get('close');
    $form['enable_escape']['#default_value'] = $data
      ->get('enable_escape');
    $form['overlay']['#default_value'] = $data
      ->get('overlay');
    $form['trigger_method']['#default_value'] = $data
      ->get('trigger_method');
    $form['trigger_selector']['#default_value'] = $data
      ->get('trigger_selector');
    $form['delay']['#default_value'] = $data
      ->get('delay');
    $form['trigger_width']['#default_value'] = $data
      ->get('trigger_width');
    $form['width']['#default_value'] = $data
      ->get('width');
    $form['cookie_expiry']['#default_value'] = $data
      ->get('cookie_expiry');
    $form['button_configuration']['show_minimized_button']['#default_value'] = $data
      ->get('show_minimized_button');
    $form['popup_frequency']['use_time_frequency']['#default_value'] = $data
      ->get('use_time_frequency');
    $form['popup_frequency']['time_frequency']['#default_value'] = $data
      ->get('time_frequency');
    $form['popup_frequency']['visit_counts']['#default_value'] = $visit_counts;
    $block_id_append = '';
    if ($data
      ->get('type') == 0) {
      $block_id_append = 'block-';
      $identifier = preg_replace('/[_]+/', '-', $identifier);
    }
    $identifier = $block_id_append . $identifier;
    $parent = "#spb-" . $identifier;
    $modal_class = "." . $identifier . "-modal";
    $modal_close_class = "." . $identifier . "-modal-close";
    $modal_minimize_class = "." . $identifier . "-modal-minimize";
    $modal_minimized_class = "." . $identifier . "-modal-minimized";
    $positions = [
      0 => $this
        ->t('spb_top_left'),
      1 => $this
        ->t('spb_top_right'),
      2 => $this
        ->t('spb_bottom_left'),
      3 => $this
        ->t('spb_bottom_right'),
      4 => $this
        ->t('spb_center'),
      5 => $this
        ->t('spb_top_center'),
      6 => $this
        ->t('spb_top_bar'),
      7 => $this
        ->t('spb_bottom_bar'),
      8 => $this
        ->t('spb_left_bar'),
      9 => $this
        ->t('spb_right_bar'),
    ];
    $override_positions = $modal_class . ' .' . $positions[$data
      ->get('layout')];
    $css_selector = '#';
    if ($data
      ->get('type') && $data
      ->get('css_selector') == 0) {
      $css_selector = '.';
    }
    $form['adjustments']['#description'] = $this
      ->t('Use the following css selectors to customize the popup designs.');
    $rows = [
      [
        'Parent',
        $parent,
      ],
      [
        'Identifier',
        $css_selector . $identifier,
      ],
      [
        'Modal class',
        $modal_class,
      ],
      [
        'Modal close class',
        $modal_close_class,
      ],
      [
        'Modal minimize class',
        $modal_minimize_class,
      ],
      [
        'Modal minimized class',
        $modal_minimized_class,
      ],
      [
        'Override positions',
        $override_positions,
      ],
    ];
    $form['adjustments']['table'] = [
      '#type' => 'table',
      '#rows' => $rows,
      '#empty' => t('No results found'),
    ];
    $documentation = Url::fromUri('https://www.drupal.org/docs/8/modules/simple-popup-blocks');
    $documentation = Link::fromTextAndUrl($this
      ->t('here'), $documentation);
    $documentation = $documentation
      ->toRenderable();
    $documentation = render($documentation);
    $display_none = "<p><h3>Add this css code in your css file (Recommended)</h3><p>";
    $display_none .= "<code>\n      " . $css_selector . $identifier . " {<br>\n      &nbsp;&nbsp;display: none;<br>\n      }</code><br>";
    $display_none .= "<p>While page loading with slow internet connection, content of popup will be visible to users. So you should add this css code in your theme or any css file.</p>";
    $display_none .= "<p>See the documentation " . $documentation . "</p>";
    $form['adjustments']['display_none'] = [
      '#markup' => $display_none,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Get a value by key.
    $first = $form_state
      ->get('simple_popup_blocks_id');
    if ($form_state
      ->getValue('type') == 0) {
      $selector = 'block_list';
      $identifier = $form_state
        ->getValue($selector);
    }
    else {
      $selector = 'custom_css';
      $identifier = $form_state
        ->getValue($selector);
    }
    if (!isset($identifier) || empty($identifier)) {
      $form_state
        ->setError($form[$selector], $this
        ->t('You should provide some css selector.'));
    }
    if ($form_state
      ->getValue('type') == 1) {

      // Get the first character using substr.
      $firstCharacter = substr($identifier, 0, 1);
      if (in_array($firstCharacter, [
        '.',
        '#',
      ])) {
        $form_state
          ->setError($form[$selector], $this
          ->t('Selector should not start with . or # in %field.', [
          '%field' => $identifier,
        ]));
      }
    }
    $trigger_method = $form_state
      ->getValue('trigger_method');
    if ($trigger_method == 1) {
      $trigger_selector = $form_state
        ->getValue('trigger_selector');

      // Get the first character using substr.
      $firstCharacter = substr($trigger_selector, 0, 1);
      if (!in_array($firstCharacter, [
        '.',
        '#',
      ])) {
        $form_state
          ->setError($form['trigger_selector'], $this
          ->t('Selector should start with . or # only in %field.', [
          '%field' => $trigger_selector,
        ]));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    if ($form_state
      ->getValue('type') == 0) {
      $identifier = $form_state
        ->getValue('block_list');
    }
    else {
      $identifier = $form_state
        ->getValue('custom_css');
    }
    $delay = $form_state
      ->getValue('delay');
    if (empty($delay) || $delay < 0) {
      $delay = 0;
    }
    $trigger_width = $form_state
      ->getValue('trigger_width');
    if (empty($trigger_width) || $trigger_width < 0) {
      $trigger_width = NULL;
    }
    $width = $form_state
      ->getValue('width');
    if (empty($width) || $width < 0) {
      $width = 400;
    }
    $cookie_expiry = $form_state
      ->getValue('cookie_expiry');
    if (strlen($cookie_expiry) == 0 || $cookie_expiry < 0) {
      $cookie_expiry = 100;
    }
    if ($form_state
      ->getValue('use_time_frequency')) {
      $visit_counts = serialize([
        0 => '0',
      ]);
    }
    else {
      $visit_counts = serialize($form_state
        ->getValue('visit_counts'));
    }
    $config = \Drupal::service('config.factory')
      ->getEditable('simple_popup_blocks.popup_' . $form_state
      ->getValue('uid'));
    $config
      ->set('identifier', $identifier)
      ->set('type', $form_state
      ->getValue('type'))
      ->set('css_selector', $form_state
      ->getValue('css_selector'))
      ->set('layout', $form_state
      ->getValue('layout'))
      ->set('visit_counts', $visit_counts)
      ->set('overlay', $form_state
      ->getValue('overlay'))
      ->set('trigger_method', $form_state
      ->getValue('trigger_method'))
      ->set('trigger_selector', $form_state
      ->getValue('trigger_selector'))
      ->set('enable_escape', $form_state
      ->getValue('enable_escape'))
      ->set('delay', $delay)
      ->set('trigger_width', $trigger_width)
      ->set('minimize', $form_state
      ->getValue('minimize'))
      ->set('close', $form_state
      ->getValue('close'))
      ->set('width', $width)
      ->set('cookie_expiry', $cookie_expiry)
      ->set('status', $form_state
      ->getValue('status'))
      ->set('use_time_frequency', $form_state
      ->getValue('use_time_frequency'))
      ->set('time_frequency', $form_state
      ->getValue('time_frequency'))
      ->set('show_minimized_button', $form_state
      ->getValue('show_minimized_button'))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SimplePopupBlocksAddForm::$database protected property Database object.
SimplePopupBlocksAddForm::$messenger protected property Messanger object. Overrides MessengerTrait::$messenger
SimplePopupBlocksAddForm::$typeManager protected property Object of EntityTypeManager.
SimplePopupBlocksAddForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SimplePopupBlocksAddForm::__construct public function SimplePopupBlocksAddForm constructor. Overrides ConfigFormBase::__construct
SimplePopupBlocksEditForm::buildForm public function Form constructor. Overrides SimplePopupBlocksAddForm::buildForm
SimplePopupBlocksEditForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides SimplePopupBlocksAddForm::getEditableConfigNames
SimplePopupBlocksEditForm::getFormId public function Returns a unique string identifying the form. Overrides SimplePopupBlocksAddForm::getFormId
SimplePopupBlocksEditForm::submitForm public function Form submission handler. Overrides SimplePopupBlocksAddForm::submitForm
SimplePopupBlocksEditForm::validateForm public function Form validation handler. Overrides SimplePopupBlocksAddForm::validateForm
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.