You are here

class SocialMediaAdminForm in Social media share 8

Class SocialMediaAdminForm.

Hierarchy

Expanded class hierarchy of SocialMediaAdminForm

1 string reference to 'SocialMediaAdminForm'
social_media.routing.yml in ./social_media.routing.yml
social_media.routing.yml

File

src/Form/SocialMediaAdminForm.php, line 15

Namespace

Drupal\social_media\Form
View source
class SocialMediaAdminForm extends ConfigFormBase {

  /**
   * An event dispatcher instance to use for configuration events.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * {@inheritdoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, EventDispatcherInterface $event_dispatcher) {
    parent::__construct($config_factory);
    $this->eventDispatcher = $event_dispatcher;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('social_media.settings');
    $social_medias = $this
      ->getSocialMedias();
    $token_types = [
      'current_page',
    ];
    foreach ($social_medias as $key => $label) {
      $form[$key] = [
        '#type' => 'details',
        '#title' => t('@social_media settings', [
          '@social_media' => $label,
        ]),
        '#open' => TRUE,
      ];
      $form[$key][$key . '_enable'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Enable'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.enable'),
        '#description' => t('Globally disabled the settings'),
      ];
      $form[$key][$key . '_api_url'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('API url'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.api_url'),
      ];

      // Handle some extra help text for the Twitter service.
      if ($key == 'twitter') {
        $form[$key][$key . '_api_url']['#description'] = t('To include Hashtags format your URL like this: https://twitter.com/intent/tweet?url=[current-page:url]&text=[text to tweet]&hashtags=[comma separated list of hashtags, with no # on them]');
      }
      $form[$key]['token_browser'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => $token_types,
        '#click_insert' => TRUE,
        '#dialog' => TRUE,
      ];

      // Handle some extra configuration for email service.
      if ($key == 'email') {
        $form[$key][$key . '_api_url']['#states'] = [
          'invisible' => [
            ':input[name="' . $key . '_enable_forward' . '"]' => [
              'checked' => TRUE,
            ],
          ],
        ];
        $form[$key][$key . '_enable_forward'] = [
          '#type' => 'checkbox',
          '#title' => $this
            ->t('Enable forward instead for client email service'),
          '#default_value' => $config
            ->get('social_media.' . $key . '.enable_forward'),
          '#description' => t('If it is checked then forward email form will open as model dialog.'),
        ];
        $form[$key][$key . '_show_forward'] = [
          '#type' => 'radios',
          '#options' => [
            1 => $this
              ->t('Model dialog'),
            2 => $this
              ->t('Separate page'),
          ],
          '#title' => $this
            ->t('Choose how you want to show forward email form'),
          '#default_value' => $config
            ->get('social_media.' . $key . '.show_forward') ? $config
            ->get('social_media.' . $key . '.show_forward') : 1,
          '#states' => [
            'visible' => [
              ':input[name="' . $key . '_enable_forward' . '"]' => [
                'checked' => TRUE,
              ],
            ],
          ],
          '#description' => t('default set as dialog popup, you can change it to show in separate page'),
        ];
      }
      $form[$key][$key . '_api_event'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Event'),
        '#options' => [
          'href' => 'href',
          'onclick' => 'onclick',
        ],
        '#default_value' => $config
          ->get('social_media.' . $key . '.api_event'),
      ];
      $form[$key][$key . '_drupalSettings'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('drupalSettings variables'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.drupalSettings'),
        '#description' => t('Defines different drupalSettings variable.Each settings in new line.eg:<br/>application_id|343434434<br/> you can get those variables in js.eg drupalSettings.social_media.application_id'),
      ];
      $form[$key][$key . '_library'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Drupal library'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.library'),
        '#description' => t('Add drupal custom library.eg: social_media/facebook'),
      ];
      $form[$key][$key . '_text'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Link text'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.text'),
        '#description' => t('Text of the link'),
      ];
      $form[$key][$key . '_default_img'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Default image'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.default_img'),
        '#description' => t('If it is checked default image will be loaded. Make service name with icon name. eg:facebook_share.svg'),
      ];
      $form[$key][$key . '_img'] = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Alternative image link'),
        '#states' => [
          'visible' => [
            ':input[name="' . $key . '_default_img' . '"]' => [
              'checked' => FALSE,
            ],
          ],
        ],
        '#default_value' => $config
          ->get('social_media.' . $key . '.img'),
        '#description' => t('If you want to have your custom image, give image path.'),
      ];
      $form[$key][$key . '_weight'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Order of share button'),
        '#max' => 10,
        '#min' => 0,
        '#default_value' => $config
          ->get('social_media.' . $key . '.weight'),
        '#description' => t('Order of the share link to render'),
      ];
      $form[$key][$key . '_attributes'] = [
        '#type' => 'textarea',
        '#title' => $this
          ->t('Attributes'),
        '#default_value' => $config
          ->get('social_media.' . $key . '.attributes'),
        '#description' => t('Defines different attributes of link. Each attribute in new line.eg:<br/>target|blank<br/> class|facebook-share js-share'),
      ];
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $socials = $this
      ->getSocialMedias();
    $config = $this
      ->config('social_media.settings');
    $config
      ->clear('social_media.settings');
    foreach ($socials as $social => $media) {
      if ($form_state
        ->hasValue($social . '_enable')) {
        $config
          ->set('social_media.' . $social . '.enable', $form_state
          ->getValue($social . '_enable'));
      }
      if ($form_state
        ->hasValue($social . '_api_url')) {
        $config
          ->set('social_media.' . $social . '.api_url', $form_state
          ->getValue($social . '_api_url'));
      }
      if ($form_state
        ->hasValue($social . '_api_event')) {
        $config
          ->set('social_media.' . $social . '.api_event', $form_state
          ->getValue($social . '_api_event'));
      }
      if ($form_state
        ->hasValue($social . '_drupalSettings')) {
        $config
          ->set('social_media.' . $social . '.drupalSettings', $form_state
          ->getValue($social . '_drupalSettings'));
      }
      if ($form_state
        ->hasValue($social . '_library')) {
        $config
          ->set('social_media.' . $social . '.library', $form_state
          ->getValue($social . '_library'));
      }
      if ($form_state
        ->hasValue($social . '_text')) {
        $config
          ->set('social_media.' . $social . '.text', $form_state
          ->getValue($social . '_text'));
      }
      if ($form_state
        ->hasValue($social . '_default_img')) {
        $config
          ->set('social_media.' . $social . '.default_img', $form_state
          ->getValue($social . '_default_img'));
      }
      if ($form_state
        ->hasValue($social . '_img')) {
        $config
          ->set('social_media.' . $social . '.img', $form_state
          ->getValue($social . '_img'));
      }
      if ($form_state
        ->hasValue($social . '_weight')) {
        $config
          ->set('social_media.' . $social . '.weight', $form_state
          ->getValue($social . '_weight'));
      }
      if ($form_state
        ->hasValue($social . '_attributes')) {
        $config
          ->set('social_media.' . $social . '.attributes', $form_state
          ->getValue($social . '_attributes'));
      }
      if ($form_state
        ->hasValue($social . '_enable_forward')) {
        $config
          ->set('social_media.' . $social . '.enable_forward', $form_state
          ->getValue($social . '_enable_forward'));
      }
      if ($form_state
        ->hasValue($social . '_show_forward')) {
        $config
          ->set('social_media.' . $social . '.show_forward', $form_state
          ->getValue($social . '_show_forward'));
      }
    }
    $config
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('Your configuration has been saved'));
  }

  /**
   * Get social media elements.
   *
   * @return array
   *   Social media elements.
   */
  protected function getSocialMedias() {
    $elements = [
      'facebook_share' => 'Facebook share',
      'facebook_msg' => 'Facebook messenger',
      'linkedin' => 'Linkedin',
      'twitter' => 'Twitter',
      'pinterest' => 'Pinterest',
      'email' => 'Email',
      'whatsapp' => 'whatsapp',
      'print' => 'Print',
    ];
    $event = new SocialMediaEvent($elements);
    $this->eventDispatcher
      ->dispatch('social_media.add_more_link', $event);
    $elements = $event
      ->getElement();
    return $elements;
  }

}

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.
SocialMediaAdminForm::$eventDispatcher protected property An event dispatcher instance to use for configuration events.
SocialMediaAdminForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SocialMediaAdminForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
SocialMediaAdminForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SocialMediaAdminForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SocialMediaAdminForm::getSocialMedias protected function Get social media elements.
SocialMediaAdminForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SocialMediaAdminForm::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 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.