You are here

class TwitterBlock in Twitter Block 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/TwitterBlock.php \Drupal\twitter_block\Plugin\Block\TwitterBlock

Defines a twitter block block type.

Plugin annotation


@Block(
  id = "twitter_block",
  admin_label = @Translation("Twitter block"),
  category = @Translation("Twitter"),
)

Hierarchy

Expanded class hierarchy of TwitterBlock

File

src/Plugin/Block/TwitterBlock.php, line 18

Namespace

Drupal\twitter_block\Plugin\Block
View source
class TwitterBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $config = $this
      ->getConfiguration();
    $form['username'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Username'),
      '#default_value' => $config['username'],
      '#required' => TRUE,
      '#field_prefix' => '@',
    ];
    $form['appearance'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Appearance'),
    ];
    $form['appearance']['theme'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Theme'),
      '#default_value' => $config['theme'],
      '#options' => [
        '' => $this
          ->t('Default'),
        'dark' => $this
          ->t('Dark'),
      ],
      '#description' => $this
        ->t('Select a theme for the widget.'),
    ];
    $form['appearance']['link_color'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Link color'),
      '#default_value' => $config['link_color'],
      '#maxlength' => 6,
      '#size' => 6,
      '#field_prefix' => '#',
      '#description' => $this
        ->t('Change the link color used by the widget.
        Takes an %format hex format color. Note that some icons in the widget
        will also appear this color.', [
        '%format' => 'abc123',
      ]),
    ];
    $form['appearance']['border_color'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Border color'),
      '#default_value' => $config['border_color'],
      '#maxlength' => 6,
      '#size' => 6,
      '#field_prefix' => '#',
      '#description' => $this
        ->t('Change the border color used by the widget.
        Takes an %format hex format color.', [
        '%format' => 'abc123',
      ]),
    ];
    $form['appearance']['chrome'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Chrome'),
      '#default_value' => $config['chrome'],
      '#options' => [
        'noheader' => $this
          ->t('No header'),
        'nofooter' => $this
          ->t('No footer'),
        'noborders' => $this
          ->t('No borders'),
        'noscrollbar' => $this
          ->t('No scrollbar'),
        'transparent' => $this
          ->t('Transparent'),
      ],
      '#description' => $this
        ->t('Control the widget layout and chrome.'),
    ];
    $form['functionality'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Functionality'),
    ];
    $form['functionality']['related'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Related users'),
      '#default_value' => $config['related'],
      '#description' => $this
        ->t('As per the Tweet and follow buttons, you may
        provide a comma-separated list of user screen names as suggested
        followers to a user after they reply, Retweet, or favorite a Tweet in the timeline.'),
    ];
    $form['functionality']['tweet_limit'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Tweet limit'),
      '#default_value' => $config['tweet_limit'],
      '#options' => [
        '' => $this
          ->t('Auto'),
      ],
      '#description' => $this
        ->t('Fix the size of a timeline to a preset number
        of Tweets between 1 and 20. The timeline will render the specified number
        of Tweets from the timeline, expanding the height of the widget to
        display all Tweets without scrolling. Since the widget is of a fixed
        size, it will not poll for updates when using this option.'),
    ];
    $form['functionality']['tweet_limit']['#options'] += array_combine(range(1, 20), range(1, 20));
    $form['size'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Size'),
      '#description' => $this
        ->t('Embedded timelines are flexible and adaptive,
        functioning at a variety of dimensions ranging from wide to narrow,
        and short to tall. The default dimensions for a timeline are 520×600px,
        which can be overridden to fit the dimension requirements of your page.
        Setting a width is not required, and by default the widget will shrink
        to the width of its parent element in the page.'),
    ];
    $form['size']['width'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Width'),
      '#default_value' => $config['width'],
      '#size' => 6,
      '#field_suffix' => 'px',
      '#description' => $this
        ->t('Change the width of the widget.'),
    ];
    $form['size']['height'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Height'),
      '#default_value' => $config['height'],
      '#size' => 6,
      '#field_suffix' => 'px',
      '#description' => $this
        ->t('Change the height of the widget.'),
    ];
    $form['size']['note'] = [
      '#type' => 'markup',
      '#markup' => '<p>' . $this
        ->t('The minimum width of a timeline is 180px
        and the maximum is 520px. The minimum height is 200px.') . '</p>',
    ];
    $form['accessibility'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Accessibility'),
    ];
    $form['accessibility']['language'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Language'),
      '#default_value' => $config['language'],
      '#maxlength' => 5,
      '#size' => 5,
      '#description' => $this
        ->t('The widget language is detected from the page,
        based on the language of your content. Enter a <a href="@website">
        language code</a> to manually override the language.', [
        '@website' => 'http://www.w3.org/TR/html401/struct/dirlang.html#h-8.1.1',
      ]),
    ];
    $form['accessibility']['polite'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('ARIA politeness'),
      '#options' => [
        'polite' => $this
          ->t('Polite'),
        'assertive' => $this
          ->t('Assertive'),
      ],
      '#default_value' => $config['polite'],
      '#description' => $this
        ->t('ARIA is an accessibility system that aids people
        using assistive technology interacting with dynamic web content.
        <a href="@website">Read more about ARIA on W3C\'s website</a>. By
        default, the embedded timeline uses the least obtrusive setting:
        "polite". If you\'re using an embedded timeline as a primary source of
        content on your page, you may wish to override this to the assertive
        setting, using "assertive".', [
        '@website' => 'http://www.w3.org/WAI/intro/aria.php',
      ]),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this
      ->setConfigurationValue('username', $form_state
      ->getValue('username'));
    foreach ([
      'appearance',
      'functionality',
      'size',
      'accessibility',
    ] as $fieldset) {
      $fieldset_values = $form_state
        ->getValue($fieldset);
      foreach ($fieldset_values as $key => $value) {
        $this
          ->setConfigurationValue($key, $value);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $config = $this
      ->getConfiguration();
    $render['block'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Tweets by @@username', [
        '@username' => $config['username'],
      ]),
      '#url' => Url::fromUri('https://twitter.com/' . $config['username']),
      '#attributes' => [
        'class' => [
          'twitter-timeline',
        ],
      ],
      '#attached' => [
        'library' => [
          'twitter_block/widgets',
        ],
      ],
    ];
    if (!empty($config['theme'])) {
      $render['block']['#attributes']['data-theme'] = $config['theme'];
    }
    if (!empty($config['link_color'])) {
      $render['block']['#attributes']['data-link-color'] = '#' . $config['link_color'];
    }
    if (!empty($config['width'])) {
      $render['block']['#attributes']['data-width'] = $config['width'];
    }
    if (!empty($config['height'])) {
      $render['block']['#attributes']['data-height'] = $config['height'];
    }
    if (!empty($config['chrome'])) {
      $options = array_keys(array_filter($config['chrome']));
      if (count($options)) {
        $render['block']['#attributes']['data-chrome'] = implode(' ', $options);
      }
    }
    if (!empty($config['border_color'])) {
      $render['block']['#attributes']['data-border-color'] = '#' . $config['border_color'];
    }
    if (!empty($config['language'])) {
      $render['block']['#attributes']['lang'] = $config['language'];
    }
    if (!empty($config['tweet_limit'])) {
      $render['block']['#attributes']['data-tweet-limit'] = $config['tweet_limit'];
    }
    if (!empty($config['related'])) {
      $render['block']['#attributes']['data-related'] = $config['related'];
    }
    if (!empty($config['polite'])) {
      $render['block']['#attributes']['aria-polite'] = $config['polite'];
    }
    return $render;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'username' => '',
      'theme' => '',
      'link_color' => '',
      'width' => '',
      'height' => '',
      'chrome' => [],
      'border_color' => '',
      'language' => '',
      'tweet_limit' => '',
      'related' => '',
      'polite' => '',
    ];
  }

}

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::blockValidate public function 3
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::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
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.
TwitterBlock::blockForm public function Overrides BlockPluginTrait::blockForm
TwitterBlock::blockSubmit public function Overrides BlockPluginTrait::blockSubmit
TwitterBlock::build public function Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface::build
TwitterBlock::defaultConfiguration public function Overrides BlockPluginTrait::defaultConfiguration
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