You are here

class TwitterButtonWidget in Twitter Embed 8

Class TwitterButtonWidget.

Hierarchy

Expanded class hierarchy of TwitterButtonWidget

3 files declare their use of TwitterButtonWidget
TwitterBlockBase.php in src/Plugin/Block/TwitterBlockBase.php
TwitterButtonBlock.php in src/Plugin/Block/TwitterButtonBlock.php
TwitterButtonFormatter.php in src/Plugin/Field/FieldFormatter/TwitterButtonFormatter.php
1 string reference to 'TwitterButtonWidget'
twitter_embed.services.yml in ./twitter_embed.services.yml
twitter_embed.services.yml
1 service uses TwitterButtonWidget
twitter_embed.button_widget in ./twitter_embed.services.yml
Drupal\twitter_embed\TwitterButtonWidget

File

src/TwitterButtonWidget.php, line 8

Namespace

Drupal\twitter_embed
View source
class TwitterButtonWidget extends TwitterWidget implements TwitterWidgetInterface {

  /**
   * {@inheritdoc}
   */
  public function getAvailableSettings() {
    return [
      'username' => '',
      'display_style' => 'follow-button',
      'display_options' => [
        'hide_username' => FALSE,
        'hide_followers_count' => FALSE,
        'size' => NULL,
        'theme' => 'light',
        'link_color' => '#2b7bb9',
        'border_color' => '#000000',
        'width' => 0,
        'height' => 600,
        'language' => '',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function getDefaultSettings() {

    // @todo flatten from getButtonAvailableSettings()
    return [
      'username' => '',
      'display_style' => 'follow-button',
      'hide_username' => FALSE,
      'hide_followers_count' => FALSE,
      'size' => NULL,
      'theme' => 'light',
      'link_color' => '#2b7bb9',
      'border_color' => '#000000',
      'width' => 0,
      'height' => 600,
      'language' => '',
      // Settings are shared amongst formatters
      // so even if their values are not used,
      // they have to be declared.
      'type' => 'profile',
      'type_value' => '',
      'chrome' => NULL,
      'tweet_limit' => 0,
      'show_replies' => FALSE,
      'aria_polite' => 'polite',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableTypes() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getAvailableDisplayStyles() {
    return [
      'follow-button' => t('Follow Button'),
      'mention-button' => t('Mention Button'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getSettingsForm(array $configuration) {
    $form = [];

    // @todo handle mention-button extra options like text.
    $form['display_style'] = [
      '#type' => 'radios',
      '#title' => t('Display style'),
      '#options' => $this
        ->getAvailableDisplayStyles(),
      '#default_value' => $configuration['display_style'],
      '#required' => TRUE,
    ];
    $form['display_options'] = [
      '#type' => 'details',
      '#title' => t('Display options'),
      '#open' => FALSE,
    ];
    $form['display_options']['hide_username'] = [
      '#type' => 'checkbox',
      '#title' => t('Hide username'),
      '#default_value' => $configuration['hide_username'],
    ];
    $form['display_options']['hide_followers_count'] = [
      '#type' => 'checkbox',
      '#title' => t('Hide followers count'),
      '#default_value' => $configuration['hide_followers_count'],
    ];
    $form['display_options']['size'] = [
      '#type' => 'checkbox',
      '#title' => t('Large button'),
      '#default_value' => $configuration['size'],
    ];
    $form['display_options']['language'] = [
      '#type' => 'select',
      '#title' => t('Language'),
      '#description' => t('What language would you like to display this in?.'),
      '#options' => $this
        ->getAvailableLanguages(),
      '#default_value' => $configuration['language'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function setSettingsFormStates(array $form, $selector) {
    $form['display_options']['hide_username']['#states'] = [
      'visible' => [
        [
          'input[name="' . $selector . '[display_style]"]' => [
            'value' => 'follow-button',
          ],
        ],
      ],
    ];
    $form['display_options']['hide_followers_count']['#states'] = [
      'visible' => [
        [
          'input[name="' . $selector . '[display_style]"]' => [
            'value' => 'follow-button',
          ],
        ],
      ],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function setDependentConfiguration(array &$configuration) {

    // @todo these rules should be used in form validation.
    // Uncheck the hide_username and hide_followers_count when unnecessary.
    if (!in_array($configuration['display_style'], [
      'follow-button',
    ])) {
      $configuration['hide_username'] = FALSE;
      $configuration['hide_followers_count'] = FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwitterButtonWidget::getAvailableDisplayStyles public function Get all available display styles. Overrides TwitterWidgetInterface::getAvailableDisplayStyles
TwitterButtonWidget::getAvailableSettings public function Get all available settings for a widget. Overrides TwitterWidgetInterface::getAvailableSettings
TwitterButtonWidget::getAvailableTypes public function Get all available types. Overrides TwitterWidgetInterface::getAvailableTypes
TwitterButtonWidget::getDefaultSettings public static function Get default settings for a widget. Overrides TwitterWidgetInterface::getDefaultSettings
TwitterButtonWidget::getSettingsForm public function Get the settings form for a widget. Overrides TwitterWidgetInterface::getSettingsForm
TwitterButtonWidget::setDependentConfiguration public function Set the configuration state based on the chosen options. Overrides TwitterWidgetInterface::setDependentConfiguration
TwitterButtonWidget::setSettingsFormStates public function Set the settings form #states based the context selector. Overrides TwitterWidgetInterface::setSettingsFormStates
TwitterWidget::createAttributes private function Returns attributes depending on the configuration.
TwitterWidget::createLabel private function Returns a label depending on the configuration.
TwitterWidget::createUrl private function Returns a Twitter Url depending on the configuration.
TwitterWidget::getAvailableLanguages public function Get all available languages. Overrides TwitterWidgetInterface::getAvailableLanguages
TwitterWidget::getWidget public function Returns a Twitter widget depending on the configuration. Overrides TwitterWidgetInterface::getWidget
TwitterWidgetInterface::USERNAME_MAX_LENGTH constant