You are here

class SimpleFacebookPixelSettingsForm in Simple Facebook Pixel 8

Class SimpleFacebookPixelSettingsForm.

@package Drupal\simple_facebook_pixel\Form

Hierarchy

Expanded class hierarchy of SimpleFacebookPixelSettingsForm

1 string reference to 'SimpleFacebookPixelSettingsForm'
simple_facebook_pixel.routing.yml in ./simple_facebook_pixel.routing.yml
simple_facebook_pixel.routing.yml

File

src/Form/SimpleFacebookPixelSettingsForm.php, line 17

Namespace

Drupal\simple_facebook_pixel\Form
View source
class SimpleFacebookPixelSettingsForm extends ConfigFormBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * SimpleFacebookPixelSettingsForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
    parent::__construct($config_factory);
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('module_handler'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'simple_facebook_pixel_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'simple_facebook_pixel.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('simple_facebook_pixel.settings');
    $form['pixel_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable Facebook Pixel'),
      '#default_value' => $config
        ->get('pixel_enabled'),
    ];
    $form['basic_settings'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Basic settings'),
      '#open' => FALSE,
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['basic_settings']['pixel_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Facebook Pixel ID'),
      '#description' => $this
        ->t('Your Facebook Pixel ID. Separate multiple Pixels with a comma.'),
      '#default_value' => $config
        ->get('pixel_id'),
      '#required' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['basic_settings']['exclude_admin_pages'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Exclude admin pages'),
      '#default_value' => $config
        ->get('exclude_admin_pages'),
    ];
    $form['basic_settings']['excluded_roles'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Exclude Facebook Pixel for the following roles.'),
      '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', user_role_names()),
      '#default_value' => $config
        ->get('excluded_roles'),
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['events'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Events'),
      '#open' => FALSE,
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['events']['page_view_notice'] = [
      '#type' => 'markup',
      '#markup' => '<p>' . $this
        ->t('PageView event is by default enabled on all pages. Other events can be enabled/disabled bellow.') . '</p>',
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['events']['view_content_entities'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Add ViewContent event to the following pages'),
      '#options' => array_map('\\Drupal\\Component\\Utility\\Html::escape', $this
        ->getViewContentEntities()),
      '#default_value' => $config
        ->get('view_content_entities'),
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['events']['complete_registration_notice'] = [
      '#type' => 'markup',
      '#markup' => '<strong>' . $this
        ->t('CompleteRegistration') . '</strong>',
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['events']['complete_registration_enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable'),
      '#default_value' => $config
        ->get('complete_registration_enabled'),
      '#states' => [
        'visible' => [
          ':input[name="pixel_enabled"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    if ($this->moduleHandler
      ->moduleExists('commerce_checkout')) {
      $form['events']['initiate_checkout_notice'] = [
        '#type' => 'markup',
        '#markup' => '<strong>' . $this
          ->t('Initiate Checkout') . '</strong>',
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['events']['initiate_checkout_enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable'),
        '#default_value' => $config
          ->get('initiate_checkout_enabled'),
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['events']['purchase_notice'] = [
        '#type' => 'markup',
        '#markup' => '<strong>' . $this
          ->t('Purchase') . '</strong>',
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['events']['purchase_enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable'),
        '#default_value' => $config
          ->get('purchase_enabled'),
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['events']['add_to_cart_notice'] = [
        '#type' => 'markup',
        '#markup' => '<strong>' . $this
          ->t('AddToCart') . '</strong>',
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['events']['add_to_cart_enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable'),
        '#default_value' => $config
          ->get('add_to_cart_enabled'),
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    if ($this->moduleHandler
      ->moduleExists('commerce_wishlist') || $this->moduleHandler
      ->moduleExists('flag')) {
      $form['events']['add_to_wishlist_notice'] = [
        '#type' => 'markup',
        '#markup' => '<strong>' . $this
          ->t('AddToWishlist') . '</strong>',
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    if ($this->moduleHandler
      ->moduleExists('commerce_wishlist')) {
      $form['events']['add_to_wishlist_enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable (using Commerce Wishlist module)'),
        '#default_value' => $config
          ->get('add_to_wishlist_enabled'),
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
    }
    if ($this->moduleHandler
      ->moduleExists('flag')) {
      $form['events']['add_to_wishlist_flag_enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable (using Flag module)'),
        '#default_value' => $config
          ->get('add_to_wishlist_flag_enabled'),
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ];
      $form['events']['add_to_wishlist_flag_list'] = [
        '#type' => 'checkboxes',
        '#options' => $this
          ->getFlags(),
        '#title' => $this
          ->t('Available flags'),
        '#default_value' => $config
          ->get('add_to_wishlist_flag_list'),
        '#states' => [
          'visible' => [
            ':input[name="pixel_enabled"]' => [
              'checked' => TRUE,
            ],
            ':input[name="add_to_wishlist_flag_enabled"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
        '#prefix' => '<div class="available-flags">',
        '#suffix' => '</div>',
      ];
    }
    $form['#attached']['library'][] = 'simple_facebook_pixel/simple_facebook_pixel.admin';
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $config = $this
      ->config('simple_facebook_pixel.settings')
      ->set('pixel_enabled', $values['pixel_enabled'])
      ->set('pixel_id', $values['pixel_id'])
      ->set('exclude_admin_pages', $values['exclude_admin_pages'])
      ->set('excluded_roles', $values['excluded_roles'])
      ->set('view_content_entities', $values['view_content_entities'])
      ->set('complete_registration_enabled', $values['complete_registration_enabled']);
    if ($this->moduleHandler
      ->moduleExists('commerce_checkout')) {
      $config
        ->set('initiate_checkout_enabled', $values['initiate_checkout_enabled'])
        ->set('purchase_enabled', $values['purchase_enabled'])
        ->set('add_to_cart_enabled', $values['add_to_cart_enabled']);
    }
    if ($this->moduleHandler
      ->moduleExists('commerce_wishlist')) {
      $config
        ->set('add_to_wishlist_enabled', $values['add_to_wishlist_enabled']);
    }
    if ($this->moduleHandler
      ->moduleExists('flag')) {
      $config
        ->set('add_to_wishlist_flag_enabled', $values['add_to_wishlist_flag_enabled'])
        ->set('add_to_wishlist_flag_list', $values['add_to_wishlist_flag_list']);
    }
    $config
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Gets content entities for which it is possible to enable ViewContent event.
   *
   * @return array
   *   The list of entities.
   */
  protected function getViewContentEntities() {
    $result = [];
    if ($this->entityTypeManager
      ->hasDefinition('node_type')) {
      $content_types = $this->entityTypeManager
        ->getStorage('node_type')
        ->loadMultiple();
      foreach ($content_types as $content_type) {
        $result['node:' . $content_type
          ->getOriginalId()] = $this
          ->t('Node: @label', [
          '@label' => $content_type
            ->label(),
        ]);
      }
    }
    if ($this->entityTypeManager
      ->hasDefinition('taxonomy_vocabulary')) {
      $vocabularies = $this->entityTypeManager
        ->getStorage('taxonomy_vocabulary')
        ->loadMultiple();
      foreach ($vocabularies as $vocabulary) {
        $result['taxonomy_term:' . $vocabulary
          ->getOriginalId()] = $this
          ->t('Taxonomy: @label', [
          '@label' => $vocabulary
            ->label(),
        ]);
      }
    }
    if ($this->entityTypeManager
      ->hasDefinition('commerce_product')) {
      $commerce_products = $this->entityTypeManager
        ->getStorage('commerce_product_type')
        ->loadMultiple();
      foreach ($commerce_products as $commerce_product) {
        $result['commerce_product:' . $commerce_product
          ->getOriginalId()] = $this
          ->t('Commerce Product: @label', [
          '@label' => $commerce_product
            ->label(),
        ]);
      }
    }
    return $result;
  }

  /**
   * Gets a list of available flags.
   *
   * @return array
   *   An array of flags.
   */
  protected function getFlags() {
    $flags_list = [];
    if ($this->moduleHandler
      ->moduleExists('flag')) {
      $flags = \Drupal::service('flag')
        ->getAllFlags('commerce_product');
      foreach ($flags as $flag) {
        $flags_list[$flag
          ->id()] = $flag
          ->label();
      }
    }
    return $flags_list;
  }

}

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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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 protected property The messenger. 29
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.
SimpleFacebookPixelSettingsForm::$entityTypeManager protected property The entity type manager.
SimpleFacebookPixelSettingsForm::$moduleHandler protected property The module handler.
SimpleFacebookPixelSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SimpleFacebookPixelSettingsForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SimpleFacebookPixelSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SimpleFacebookPixelSettingsForm::getFlags protected function Gets a list of available flags.
SimpleFacebookPixelSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SimpleFacebookPixelSettingsForm::getViewContentEntities protected function Gets content entities for which it is possible to enable ViewContent event.
SimpleFacebookPixelSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SimpleFacebookPixelSettingsForm::__construct public function SimpleFacebookPixelSettingsForm constructor. Overrides ConfigFormBase::__construct
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.