You are here

public function Twitter::buildConfigurationForm in Media entity Twitter 8.2

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides MediaSourceBase::buildConfigurationForm

File

src/Plugin/media/Source/Twitter.php, line 301

Class

Twitter
Twitter entity media source.

Namespace

Drupal\media_entity_twitter\Plugin\media\Source

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['use_twitter_api'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Whether to use Twitter api to fetch tweets or not.'),
    '#description' => $this
      ->t("In order to use Twitter's api you have to create a developer account and an application. For more information consult the readme file."),
    '#default_value' => empty($this->configuration['use_twitter_api']) ? 0 : $this->configuration['use_twitter_api'],
    '#options' => [
      0 => $this
        ->t('No'),
      1 => $this
        ->t('Yes'),
    ],
  ];

  // @todo: Evaluate if this should be a site-wide configuration.
  $form['consumer_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Consumer key'),
    '#default_value' => empty($this->configuration['consumer_key']) ? NULL : $this->configuration['consumer_key'],
    '#states' => [
      'visible' => [
        ':input[name="source_configuration[use_twitter_api]"]' => [
          'value' => '1',
        ],
      ],
    ],
  ];
  $form['consumer_secret'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Consumer secret'),
    '#default_value' => empty($this->configuration['consumer_secret']) ? NULL : $this->configuration['consumer_secret'],
    '#states' => [
      'visible' => [
        ':input[name="source_configuration[use_twitter_api]"]' => [
          'value' => '1',
        ],
      ],
    ],
  ];
  $form['oauth_access_token'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Oauth access token'),
    '#default_value' => empty($this->configuration['oauth_access_token']) ? NULL : $this->configuration['oauth_access_token'],
    '#states' => [
      'visible' => [
        ':input[name="source_configuration[use_twitter_api]"]' => [
          'value' => '1',
        ],
      ],
    ],
  ];
  $form['oauth_access_token_secret'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Oauth access token secret'),
    '#default_value' => empty($this->configuration['oauth_access_token_secret']) ? NULL : $this->configuration['oauth_access_token_secret'],
    '#states' => [
      'visible' => [
        ':input[name="source_configuration[use_twitter_api]"]' => [
          'value' => '1',
        ],
      ],
    ],
  ];
  $form['generate_thumbnails'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Generate thumbnails'),
    '#default_value' => $this->configuration['generate_thumbnails'],
    '#states' => [
      'visible' => [
        ':input[name="source_configuration[use_twitter_api]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#description' => $this
      ->t('If checked, Drupal will automatically generate thumbnails for tweets that do not reference any external media. In certain circumstances, <strong>this may violate <a href="@policy">Twitter\'s fair use policy</a></strong>. Please <strong>read it and be careful</strong> if you choose to enable this.', [
      '@policy' => 'https://dev.twitter.com/overview/terms/agreement-and-policy',
    ]),
  ];
  return $form;
}