You are here

class ShariffBlock in Shariff Social Media Buttons 8

Provides a 'shariff' block.

Plugin annotation


@Block(
  id = "shariff_block",
  admin_label = @Translation("Shariff share buttons"),
  category = @Translation("Blocks"),
)

Hierarchy

Expanded class hierarchy of ShariffBlock

File

src/Plugin/Block/ShariffBlock.php, line 19

Namespace

Drupal\shariff\Plugin\Block
View source
class ShariffBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = $this
      ->getConfiguration();
    $shariff_settings = [];
    foreach ($config as $setting => $value) {

      // Only set shariff settings as variable.
      if (substr($setting, 0, strlen('shariff')) === 'shariff') {
        $shariff_settings[$setting] = $value;
      }
    }

    // Set variable when block should overwrite default settings.
    $blocksettings = isset($config['shariff_default_settings']) && $config['shariff_default_settings'] ? NULL : $shariff_settings;
    $block = [
      '#theme' => 'block_shariff',
      '#blocksettings' => $blocksettings,
      '#attached' => [
        'library' => [
          'shariff/shariff',
        ],
      ],
    ];
    return $block;
  }

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

    // Retrieve existing configuration for this block.
    $config = $this
      ->getConfiguration();
    $form['shariff_default_settings'] = [
      '#title' => $this
        ->t('Use Shariff default settings'),
      '#description' => $this
        ->t('When set default Shariff settings are used. Uncheck to overwrite settings here.'),
      '#type' => 'checkbox',
      '#default_value' => isset($config['shariff_default_settings']) ? $config['shariff_default_settings'] : TRUE,
    ];
    $form['shariff_services'] = [
      '#title' => $this
        ->t('Activated services'),
      '#description' => $this
        ->t('Please define for which services a sharing button should be included.'),
      '#type' => 'checkboxes',
      '#options' => [
        'twitter' => $this
          ->t('Twitter'),
        'facebook' => $this
          ->t('Facebook'),
        'linkedin' => $this
          ->t('LinkedIn'),
        'pinterest' => $this
          ->t('Pinterest'),
        'vk' => $this
          ->t('VK'),
        'xing' => $this
          ->t('Xing'),
        'whatsapp' => $this
          ->t('WhatsApp'),
        'addthis' => $this
          ->t('AddThis'),
        'telegram' => $this
          ->t('Telegram'),
        'tumblr' => $this
          ->t('Tumblr'),
        'flattr' => $this
          ->t('Flattr'),
        'diaspora' => $this
          ->t('Diaspora'),
        'flipboard' => $this
          ->t('Flipboard'),
        'pocket' => $this
          ->t('Pocket'),
        'print' => $this
          ->t('Print'),
        'reddit' => $this
          ->t('reddit'),
        'stumbleupon' => $this
          ->t('StumbleUpon'),
        'threema' => $this
          ->t('Threema'),
        'mail' => $this
          ->t('E-Mail'),
        'info' => $this
          ->t('Info Button'),
        'buffer' => $this
          ->t('Buffer'),
      ],
      '#default_value' => isset($config['shariff_services']) ? $config['shariff_services'] : \Drupal::config('shariff.settings')
        ->get('shariff_services'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_theme'] = [
      '#title' => $this
        ->t('Theme'),
      '#description' => $this
        ->t('Please choose a layout option.'),
      '#type' => 'radios',
      '#options' => [
        'colored' => $this
          ->t('Colored'),
        'grey' => $this
          ->t('Grey'),
        'white' => $this
          ->t('White'),
      ],
      '#default_value' => isset($config['shariff_theme']) ? $config['shariff_theme'] : \Drupal::config('shariff.settings')
        ->get('shariff_theme'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_css'] = [
      '#title' => $this
        ->t('CSS'),
      '#description' => $this
        ->t('Please choose a CSS variant. Font Awesome is used to display the services icons.'),
      '#type' => 'radios',
      '#options' => [
        'complete' => $this
          ->t('Complete (Contains also Font Awesome)'),
        'min' => $this
          ->t('Minimal (If Font Awesome is already included in your site)'),
        'naked' => $this
          ->t('None (Without any CSS)'),
      ],
      '#default_value' => isset($config['shariff_css']) ? $config['shariff_css'] : \Drupal::config('shariff.settings')
        ->get('shariff_css'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_button_style'] = [
      '#title' => $this
        ->t('Button Style'),
      '#description' => $this
        ->t('Please choose a button style.
      With "icon only" the icon is shown, with "icon-count" icon and counter and with "standard icon", text and counter are shown, depending on the display size.
      Please note: For showing counters you have to provide a working Shariff backend URL.'),
      '#type' => 'radios',
      '#options' => [
        'standard' => $this
          ->t('Standard'),
        'icon' => $this
          ->t('Icon'),
        'icon-count' => $this
          ->t('Icon Count'),
      ],
      '#default_value' => isset($config['shariff_button_style']) ? $config['shariff_button_style'] : \Drupal::config('shariff.settings')
        ->get('shariff_button_style'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_orientation'] = [
      '#title' => $this
        ->t('Orientation'),
      '#description' => $this
        ->t('Vertical will stack the buttons vertically. Default is horizontally.'),
      '#type' => 'radios',
      '#options' => [
        'vertical' => $this
          ->t('Vertical'),
        'horizontal' => $this
          ->t('Horizontal'),
      ],
      '#default_value' => isset($config['shariff_orientation']) ? $config['shariff_orientation'] : \Drupal::config('shariff.settings')
        ->get('shariff_orientation'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_twitter_via'] = [
      '#title' => $this
        ->t('Twitter Via User'),
      '#description' => $this
        ->t('Screen name of the Twitter user to attribute the Tweets to.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_twitter_via']) ? $config['shariff_twitter_via'] : \Drupal::config('shariff.settings')
        ->get('shariff_twitter_via'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_mail_url'] = [
      '#title' => $this
        ->t('Mail link'),
      '#description' => $this
        ->t('The url target used for the mail service button. Leave it as "mailto:" to let the user
 choose an email address.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_mail_url']) ? $config['shariff_mail_url'] : \Drupal::config('shariff.settings')
        ->get('shariff_mail_url'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_mail_subject'] = [
      '#title' => $this
        ->t('Mail subject'),
      '#description' => $this
        ->t("If a mailto: link is provided in Mail link above, then this value is used as the mail subject.\n Left empty the page's current (canonical) URL or og:url is used."),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_mail_subject']) ? $config['shariff_mail_subject'] : \Drupal::config('shariff.settings')
        ->get('shariff_mail_subject'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_mail_body'] = [
      '#title' => $this
        ->t('Mail body'),
      '#description' => $this
        ->t("If a mailto: link is provided in Mail link above, then this value is used as the mail body.\n Left empty the page title is used."),
      '#type' => 'textarea',
      '#default_value' => isset($config['shariff_mail_body']) ? $config['shariff_mail_body'] : \Drupal::config('shariff.settings')
        ->get('shariff_mail_body'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_referrer_track'] = [
      '#title' => $this
        ->t('Referrer track code'),
      '#description' => $this
        ->t('A string that will be appended to the share url. Disabled when empty.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_referrer_track']) ? $config['shariff_referrer_track'] : \Drupal::config('shariff.settings')
        ->get('shariff_referrer_track'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_backend_url'] = [
      '#title' => $this
        ->t('Backend URL'),
      '#description' => $this
        ->t('The path to your Shariff backend. Leaving the value blank disables the backend feature and no counts will occur.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_backend_url']) ? $config['shariff_backend_url'] : \Drupal::config('shariff.settings')
        ->get('shariff_backend_url'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_flattr_category'] = [
      '#title' => $this
        ->t('Flattr category'),
      '#description' => $this
        ->t('Category to be used for Flattr.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_flattr_category']) ? $config['shariff_flattr_category'] : \Drupal::config('shariff.settings')
        ->get('shariff_flattr_category'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_flattr_user'] = [
      '#title' => $this
        ->t('Flattr user'),
      '#description' => $this
        ->t('User that receives Flattr donation.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_flattr_user']) ? $config['shariff_flattr_user'] : \Drupal::config('shariff.settings')
        ->get('shariff_flattr_user'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_media_url'] = [
      '#title' => $this
        ->t('Media url'),
      '#description' => $this
        ->t('Media url to be shared (Pinterest).'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_media_url']) ? $config['shariff_media_url'] : \Drupal::config('shariff.settings')
        ->get('shariff_media_url'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_info_url'] = [
      '#title' => $this
        ->t('Shariff Information URL'),
      '#description' => $this
        ->t('The url for information about Shariff. Used by the Info Button.'),
      '#type' => 'url',
      '#default_value' => isset($config['shariff_info_url']) ? $config['shariff_info_url'] : \Drupal::config('shariff.settings')
        ->get('shariff_info_url'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_info_display'] = [
      '#title' => $this
        ->t('Shariff Information Page Display'),
      '#description' => $this
        ->t('How the above URL should be opened. Please choose a display option.'),
      '#type' => 'radios',
      '#options' => [
        'blank' => $this
          ->t('Blank'),
        'popup' => $this
          ->t('Popup'),
        'self' => $this
          ->t('Self'),
      ],
      '#default_value' => isset($config['shariff_info_display']) ? $config['shariff_info_display'] : \Drupal::config('shariff.settings')
        ->get('shariff_info_display'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_title'] = [
      '#title' => $this
        ->t('WhatsApp/Twitter Share Title'),
      '#description' => $this
        ->t('Fixed title to be used as share text in Twitter/Whatsapp.
      Normally you want to leave it as it is, then page\'s DC.title/DC.creator or page title is used.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_title']) ? $config['shariff_title'] : \Drupal::config('shariff.settings')
        ->get('shariff_title'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    $form['shariff_url'] = [
      '#title' => $this
        ->t('Canonical URL'),
      '#description' => $this
        ->t('You can fix the canonical URL of the page to check here.
         Normally you want to leave it as it is, then the page\'s canonical URL or og:url or current URL is used.'),
      '#type' => 'textfield',
      '#default_value' => isset($config['shariff_url']) ? $config['shariff_url'] : \Drupal::config('shariff.settings')
        ->get('shariff_url'),
      '#states' => [
        // Only show this field when the 'shariff_default_settings' checkbox
        // is enabled.
        'visible' => [
          ':input[name="settings[shariff_default_settings]"]' => [
            'checked' => FALSE,
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {

    // Save our custom settings when the form is submitted.
    $values = $form_state
      ->getValues();
    $this
      ->setConfigurationValue('shariff_default_settings', $form_state
      ->getValue('shariff_default_settings'));

    // Only save values when default settings should be overwritten.
    if (!$form_state
      ->getValue('shariff_default_settings')) {
      foreach ($values as $setting => $value) {
        $this
          ->setConfigurationValue($setting, $form_state
          ->getValue($setting));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function blockValidate($form, FormStateInterface $form_state) {
    $backend_url = $form_state
      ->getValue('shariff_backend_url');
    if ($backend_url && !UrlHelper::isValid($backend_url, TRUE)) {
      $this
        ->messenger()
        ->addError('Please enter a valid Backend URL.');

      // TODO: Get rid of drupal_set_message() when
      // https://www.drupal.org/node/2537732 is fixed.
      // setErrorByName is not working for now.
      $form_state
        ->setErrorByName('shariff_backend_url', t('Please enter a valid URL.'));
    }
  }

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

    // The shariff block must be cached per URL, as the URL will be shared.
    return Cache::mergeContexts(parent::getCacheContexts(), [
      'url',
    ]);
  }

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

    // The block output is dependent on the shariff settings form.
    return Cache::mergeTags(parent::getCacheTags(), [
      'config:shariff.settings',
    ]);
  }

}

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::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
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
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
ShariffBlock::blockForm public function Overrides BlockPluginTrait::blockForm
ShariffBlock::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
ShariffBlock::blockValidate public function Overrides BlockPluginTrait::blockValidate
ShariffBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
ShariffBlock::getCacheContexts public function The cache contexts associated with this object. Overrides ContextAwarePluginBase::getCacheContexts
ShariffBlock::getCacheTags public function The cache tags associated with this object. Overrides ContextAwarePluginBase::getCacheTags
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