class TwitterBlock in Twitter Block 8.2
Same name and namespace in other branches
- 8.3 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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\twitter_block\Plugin\Block\TwitterBlock
- class \Drupal\Core\Block\BlockBase implements BlockPluginInterface, PluginWithFormsInterface, PreviewFallbackInterface uses BlockPluginTrait, ContextAwarePluginAssignmentTrait
- class \Drupal\Core\Plugin\ContextAwarePluginBase implements CacheableDependencyInterface, ContextAwarePluginInterface uses DependencySerializationTrait, StringTranslationTrait, TypedDataTrait
- class \Drupal\Component\Plugin\ContextAwarePluginBase implements ContextAwarePluginInterface
Expanded class hierarchy of TwitterBlock
File
- src/
Plugin/ Block/ TwitterBlock.php, line 18
Namespace
Drupal\twitter_block\Plugin\BlockView source
class TwitterBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$config = $this
->getConfiguration();
$form['widget_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('Widget ID'),
'#default_value' => $config['widget_id'],
'#required' => TRUE,
'#description' => $this
->t('Each Twitter Block block requires a unique
widget ID which determines, among other things, the source (user
timeline, favourites, list or search) of the tweets to display. You can
view a list of your existing embedded timeline widgets (and their widget
IDs) or create new embedded timeline widgets by visiting the
<a href="@widgets_section">widgets section of your Twitter settings
page</a> (make sure that you\'re logged in). You can determine a
widget\'s ID by editing it and inspecting the URL (which should be in
the form of twitter.com/settings/widgets/WIDGET_ID/edit) or by looking
at the widget\'s embed code (look for data-widget-id="WIDGET_ID").', [
'@widgets_section' => 'https://twitter.com/settings/widgets',
]),
];
$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'),
] + [
array_combine(range(1, 20), range(1, 20)),
],
'#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['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('widget_id', $form_state
->getValue('widget_id'));
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' => 'Twitter feed',
'#url' => Url::fromUri('https://twitter.com/twitterapi'),
'#attributes' => [
'class' => [
'twitter-timeline',
],
'data-widget-id' => $config['widget_id'],
],
'#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']['width'] = $config['width'];
}
if (!empty($config['height'])) {
$render['block']['#attributes']['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 [
'widget_id' => '',
'theme' => '',
'link_color' => '',
'width' => '',
'height' => '',
'chrome' => [],
'border_color' => '',
'language' => '',
'tweet_limit' => '',
'related' => '',
'polite' => '',
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockPluginInterface:: |
constant | Indicates the block label (title) should be displayed to end users. | ||
BlockPluginTrait:: |
protected | property | The transliteration service. | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
protected | function | Returns generic default configuration for block plugins. | |
BlockPluginTrait:: |
protected | function | Indicates whether the block should be shown. | 16 |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
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:: |
public | function | ||
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 1 | |
BlockPluginTrait:: |
public | function | 3 | |
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | ||
BlockPluginTrait:: |
public | function | Sets the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add submission handling for a specific block type, override BlockBase::blockSubmit(). | |
BlockPluginTrait:: |
protected | function | Wraps the transliteration service. | |
BlockPluginTrait:: |
public | function | Most block plugins should not override this method. To add validation for a specific block type, override BlockBase::blockValidate(). | 1 |
BlockPluginTrait:: |
public | function | 22 | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Builds a form element for assigning a context to a given slot. | |
ContextAwarePluginAssignmentTrait:: |
protected | function | Wraps the context handler. | |
ContextAwarePluginBase:: |
protected | property | The data objects representing the context of this plugin. | |
ContextAwarePluginBase:: |
private | property | Data objects representing the contexts passed in the plugin configuration. | |
ContextAwarePluginBase:: |
protected | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
9 |
ContextAwarePluginBase:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
7 |
ContextAwarePluginBase:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
4 |
ContextAwarePluginBase:: |
public | function |
This code is identical to the Component in order to pick up a different
Context class. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the value for a defined context. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Gets the values for all defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Set a context on this plugin. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function |
Sets the value for a defined context. Overrides ContextAwarePluginBase:: |
|
ContextAwarePluginBase:: |
public | function |
Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface:: |
|
ContextAwarePluginBase:: |
public | function | Implements magic __get() method. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginWithFormsTrait:: |
public | function | ||
PluginWithFormsTrait:: |
public | function | ||
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TwitterBlock:: |
public | function |
Overrides BlockPluginTrait:: |
|
TwitterBlock:: |
public | function |
Overrides BlockPluginTrait:: |
|
TwitterBlock:: |
public | function |
Builds and returns the renderable array for this block plugin. Overrides BlockPluginInterface:: |
|
TwitterBlock:: |
public | function |
Overrides BlockPluginTrait:: |
|
TypedDataTrait:: |
protected | property | The typed data manager used for creating the data types. | |
TypedDataTrait:: |
public | function | Gets the typed data manager. | 2 |
TypedDataTrait:: |
public | function | Sets the typed data manager. | 2 |