You are here

class FloatingSocialIconsBlock in Floating Social Icons 8

Provides a 'Floating Social Icon' Block.

Plugin annotation


@Block(
  id = "floating_icons",
  admin_label = @Translation("Floating Social Block"),
  category = @Translation("social icon"),
)

Hierarchy

Expanded class hierarchy of FloatingSocialIconsBlock

File

src/Plugin/Block/FloatingSocialIconsBlock.php, line 17

Namespace

Drupal\floating_social_icons\Plugin\Block
View source
class FloatingSocialIconsBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {

    // Load the configuration from the form.
    $config = $this
      ->getConfiguration();
    $facebook = isset($config['facebook']) ? $config['facebook'] : '';
    $google_plus = isset($config['google_plus']) ? $config['google_plus'] : '';
    $linkedIn = isset($config['linkedIn']) ? $config['linkedIn'] : '';
    $twitter = isset($config['twitter']) ? $config['twitter'] : '';
    $pinterest = isset($config['pinterest']) ? $config['pinterest'] : '';
    $instagram = isset($config['instagram']) ? $config['instagram'] : '';
    $mail = isset($config['mail']) ? $config['mail'] : '';
    $youtube = isset($config['youtube']) ? $config['youtube'] : '';
    $icons = isset($config['place']) ? $config['place'] : '';
    $count = isset($config['count']) ? $config['count'] : '';
    $target = isset($config['target']) ? $config['target'] : '';
    $hover = isset($config['hover']) ? $config['hover'] : '';

    // Check title field for empty.
    $facebook_title = $config['facebook_title'] ?: 'Facebook';
    $google_plus_title = $config['google_plus_title'] ?: 'GooglePlus';
    $linkedIn_title = $config['linkedIn_title'] ?: 'LinkedIn';
    $twitter_title = $config['twitter_title'] ?: 'Twitter';
    $pinterest_title = $config['pinterest_title'] ?: 'Pinterest';
    $instagram_title = $config['instagram_title'] ?: 'Instagram';
    $mail_title = $config['mail_title'] ?: 'Mail';
    $youtube_title = $config['youtube_title'] ?: 'Youtube';
    $social_values = [
      'facebook' => $facebook,
      'google_plus' => $google_plus,
      'linkedIn' => $linkedIn,
      'twitter' => $twitter,
      'pinterest' => $pinterest,
      'instagram' => $instagram,
      'mail' => $mail,
      'youtube' => $youtube,
      'icons' => $icons,
      'count' => $count,
      'target' => $target,
      'hover' => $hover,
    ];
    $social_titles = [
      'facebook_title' => $facebook_title,
      'google_plus_title' => $google_plus_title,
      'linkedIn_title' => $linkedIn_title,
      'twitter_title' => $twitter_title,
      'pinterest_title' => $pinterest_title,
      'instagram_title' => $instagram_title,
      'youtube_title' => $youtube_title,
      'mail_title' => $mail_title,
    ];
    return [
      '#theme' => 'floating_social_icons_display',
      '#social_values' => $social_values,
      '#social_titles' => $social_titles,
      '#attached' => [
        'library' => [
          'floating_social_icons/floating_social_icons',
        ],
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form = parent::blockForm($form, $form_state);

    // Facebook details.
    $form['floating_facebook'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Facebook settings'),
      '#collapsible' => TRUE,
      '#open' => TRUE,
      '#description' => '',
    ];
    $form['floating_facebook']['facebook_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Facebook Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['facebook']) ? $this->configuration['facebook'] : '',
    ];
    $form['floating_facebook']['facebook_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Facebook title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['facebook_title']) ? $this->configuration['facebook_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // Twitter details.
    $form['floating_twitter'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Twitter settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '',
    ];
    $form['floating_twitter']['twitter_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Twitter Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['twitter']) ? $this->configuration['twitter'] : '',
    ];
    $form['floating_twitter']['twitter_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Twitter title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['twitter_title']) ? $this->configuration['twitter_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // Google Plus details.
    $form['floating_google_plus'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Google Plus settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => '',
    ];
    $form['floating_google_plus']['google_plus_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Google Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['google_plus']) ? $this->configuration['google_plus'] : '',
    ];
    $form['floating_google_plus']['google_plus_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('GooglePlus title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['google_plus_title']) ? $this->configuration['google_plus_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // LinkedIn details.
    $form['floating_linkedIn'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('LinkedIn settings'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#description' => '',
    ];
    $form['floating_linkedIn']['linkedIn_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('LinkedIn Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['linkedIn']) ? $this->configuration['linkedIn'] : '',
    ];
    $form['floating_linkedIn']['linkedIn_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('LinkedIn title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['linkedIn_title']) ? $this->configuration['linkedIn_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // Pinterest details.
    $form['floating_pinterest'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Pinterest settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '',
    ];
    $form['floating_pinterest']['pinterest_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Pinterest Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['pinterest']) ? $this->configuration['pinterest'] : '',
    ];
    $form['floating_pinterest']['pinterest_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Pinterest title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['pinterest_title']) ? $this->configuration['pinterest_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // Instagram details.
    $form['floating_instagram'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Instagram settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '',
    ];
    $form['floating_instagram']['instagram_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Instagram Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['instagram']) ? $this->configuration['instagram'] : '',
    ];
    $form['floating_instagram']['instagram_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Instagram title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['instagram_title']) ? $this->configuration['instagram_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // YouTube details.
    $form['floating_youtube'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Youtube settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '',
    ];
    $form['floating_youtube']['youtube_link'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('Youtube Link'),
      '#size' => 60,
      '#default_value' => isset($this->configuration['youtube']) ? $this->configuration['youtube'] : '',
    ];
    $form['floating_youtube']['youtube_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Youtube title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['youtube_title']) ? $this->configuration['youtube_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // Mail details.
    $form['floating_mail'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('mail settings'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '',
    ];
    $form['floating_mail']['mail_link'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('Mail Link'),
      '#size' => 60,
      '#description' => $this
        ->t('Please type only the e-mail id'),
      '#default_value' => isset($this->configuration['mail']) ? $this->configuration['mail'] : '',
    ];
    $form['floating_mail']['mail_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Mail title'),
      '#description' => $this
        ->t('Text to display when in active or hover state'),
      '#default_value' => isset($this->configuration['mail_title']) ? $this->configuration['mail_title'] : '',
      '#size' => 60,
      '#maxlength' => 128,
    ];

    // Block details.
    $form['floating_icons'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Display Icons'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => '',
    ];
    $form['floating_icons']['hover'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Select the Hover Effects'),
      '#required' => TRUE,
      '#attributes' => array(
        'class' => array(
          'floating-block',
        ),
      ),
      '#default_value' => isset($this->configuration['hover']) ? $this->configuration['hover'] : 'grow',
      '#options' => [
        'grow' => $this
          ->t('Grow'),
        'shrink' => $this
          ->t('Shrink'),
        'black-white' => $this
          ->t('Black and white'),
        'white-black' => $this
          ->t('White and black'),
        'rotate' => $this
          ->t('Rotate 360'),
      ],
    ];
    $form['floating_icons']['place'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Where do you want to display the icons?'),
      '#required' => TRUE,
      '#default_value' => isset($this->configuration['place']) ? $this->configuration['place'] : 4,
      '#options' => [
        1 => $this
          ->t('Top'),
        2 => $this
          ->t('Right'),
        3 => $this
          ->t('Bottom'),
        4 => $this
          ->t('Left'),
      ],
    ];
    $form['floating_icons']['target'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Target Attribute'),
      '#default_value' => isset($this->configuration['target']) ? $this->configuration['target'] : '_self',
      '#options' => [
        '_self' => $this
          ->t('_Self'),
        '_blank' => $this
          ->t('_Blank'),
      ],
    ];
    $form['floating_icons']['count'] = [
      '#title' => $this
        ->t('Count'),
      '#value' => isset($this->configuration['count']) ? $this->configuration['count'] : 0,
      '#type' => 'hidden',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockValidate($form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $links = [];
    $links[] = $values['floating_facebook']['facebook_link'];
    $links[] = $values['floating_twitter']['twitter_link'];
    $links[] = $values['floating_google_plus']['google_plus_link'];
    $links[] = $values['floating_linkedIn']['linkedIn_link'];
    $links[] = $values['floating_pinterest']['pinterest_link'];
    $links[] = $values['floating_instagram']['instagram_link'];
    $links[] = $values['floating_mail']['mail_link'];
    $links[] = $values['floating_youtube']['youtube_link'];
    $count = 0;
    if ($links) {
      foreach ($links as $link) {
        if (!empty($link)) {
          $count = $count + 1;
        }
      }
    }
    if ($count < 2) {
      $form_state
        ->setErrorByName('floatingsocialblock', $this
        ->t('At least two fields should be filled.'));
    }

    // Setting count value.
    $this->configuration['count'] = $count;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    parent::blockSubmit($form, $form_state);
    $values = $form_state
      ->getValues();
    $this->configuration['facebook'] = $values['floating_facebook']['facebook_link'];
    $this->configuration['google_plus'] = $values['floating_google_plus']['google_plus_link'];
    $this->configuration['linkedIn'] = $values['floating_linkedIn']['linkedIn_link'];
    $this->configuration['twitter'] = $values['floating_twitter']['twitter_link'];
    $this->configuration['pinterest'] = $values['floating_pinterest']['pinterest_link'];
    $this->configuration['instagram'] = $values['floating_instagram']['instagram_link'];
    $this->configuration['mail'] = $values['floating_mail']['mail_link'];
    $this->configuration['youtube'] = $values['floating_youtube']['youtube_link'];
    $this->configuration['place'] = $values['floating_icons']['place'];
    $this->configuration['target'] = $values['floating_icons']['target'];
    $this->configuration['hover'] = $values['floating_icons']['hover'];

    // Setting Config for Titles.
    $this->configuration['facebook_title'] = $values['floating_facebook']['facebook_title'];
    $this->configuration['google_plus_title'] = $values['floating_google_plus']['google_plus_title'];
    $this->configuration['linkedIn_title'] = $values['floating_linkedIn']['linkedIn_title'];
    $this->configuration['twitter_title'] = $values['floating_twitter']['twitter_title'];
    $this->configuration['pinterest_title'] = $values['floating_pinterest']['pinterest_title'];
    $this->configuration['instagram_title'] = $values['floating_instagram']['instagram_title'];
    $this->configuration['mail_title'] = $values['floating_mail']['mail_title'];
    $this->configuration['youtube_title'] = $values['floating_youtube']['youtube_title'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockPluginInterface::BLOCK_LABEL_VISIBLE constant Indicates the block label (title) should be displayed to end users.
BlockPluginTrait::$transliteration protected property The transliteration service.
BlockPluginTrait::access public function
BlockPluginTrait::baseConfigurationDefaults protected function Returns generic default configuration for block plugins.
BlockPluginTrait::blockAccess protected function Indicates whether the block should be shown. 16
BlockPluginTrait::buildConfigurationForm public function Creates a generic configuration form for all block types. Individual block plugins can add elements to this form by overriding BlockBase::blockForm(). Most block plugins should not override this method unless they need to alter the generic form elements. 2
BlockPluginTrait::calculateDependencies public function
BlockPluginTrait::defaultConfiguration public function 19
BlockPluginTrait::getConfiguration public function 1
BlockPluginTrait::getMachineNameSuggestion public function 1
BlockPluginTrait::getPreviewFallbackString public function 3
BlockPluginTrait::label public function
BlockPluginTrait::setConfiguration public function
BlockPluginTrait::setConfigurationValue public function
BlockPluginTrait::setTransliteration public function Sets the transliteration service.
BlockPluginTrait::submitConfigurationForm public function Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit().
BlockPluginTrait::transliteration protected function Wraps the transliteration service.
BlockPluginTrait::validateConfigurationForm public function Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). 1
BlockPluginTrait::__construct public function 22
ContextAwarePluginAssignmentTrait::addContextAssignmentElement protected function Builds a form element for assigning a context to a given slot.
ContextAwarePluginAssignmentTrait::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
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
FloatingSocialIconsBlock::blockForm public function Overrides BlockPluginTrait::blockForm
FloatingSocialIconsBlock::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
FloatingSocialIconsBlock::blockValidate public function Overrides BlockPluginTrait::blockValidate
FloatingSocialIconsBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
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.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
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.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2