View source
<?php
namespace Drupal\twitter_profile_widget\Form;
use Drupal\Core\Entity\ContentEntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
class TwitterWidgetForm extends ContentEntityForm {
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$entity = $this->entity;
$config = \Drupal::config('twitter_profile_widget.settings');
if ($config
->get('twitter_widget_token') !== 'Credentials Valid') {
drupal_set_message(t('Credentials for the Twitter API have not been configured or are invalid. Review the <a href=":widget">Twitter widget</a> settings.', [
':widget' => '/admin/config/media/twitter_profile_widget',
]), 'warning');
}
$form['search']['#states'] = [
'visible' => [
'.field--name-type.field--widget-options-select select:first' => [
[
'value' => 'search',
],
],
],
];
$form['account']['#states'] = [
'invisible' => [
'.field--name-type.field--widget-options-select select:first' => [
[
'value' => 'search',
],
],
],
];
$form['timeline']['#states'] = [
'visible' => [
'.field--name-type.field--widget-options-select select:first' => [
[
'value' => 'timeline',
],
],
],
];
$form['replies']['#states'] = [
'visible' => [
'.field--name-type.field--widget-options-select select:first' => [
[
'value' => 'status',
],
[
'value' => 'timeline',
],
],
],
];
$form['retweets']['#states'] = [
'visible' => [
'.field--name-type.field--widget-options-select select:first' => [
[
'value' => 'status',
],
[
'value' => 'timeline',
],
],
],
];
$form['search_parameters'] = [
'#type' => 'fieldset',
];
$form['search_parameters']['type'] = $form['type'];
$form['search_parameters']['account'] = $form['account'];
$form['search_parameters']['timeline'] = $form['timeline'];
$form['search_parameters']['search'] = $form['search'];
$form['search_parameters']['replies'] = $form['replies'];
$form['search_parameters']['retweets'] = $form['retweets'];
unset($form['search']);
unset($form['type']);
unset($form['account']);
unset($form['timeline']);
unset($form['replies']);
unset($form['retweets']);
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$entity = $this
->buildEntity($form, $form_state);
$account = $entity
->get('account')->value;
$type = $entity
->get('type')->value;
$search = $entity
->get('search')->value;
$timeline = $entity
->get('timeline')->value;
$count = $entity
->get('count')->value;
$replies = $entity
->get('replies')->value;
$retweets = $entity
->get('retweets')->value;
if ($type == 'search' && $search == '') {
$form_state
->setErrorByName('search', $this
->t('The "Search term" type requires entering a search parameter.'));
}
if ($type !== 'search' && $account == '') {
$form_state
->setErrorByName('account', $this
->t('This Twitter widget type requires that you enter an account handle.'));
}
if ($type == 'timeline' && $timeline == '') {
$form_state
->setErrorByName('timeline', $this
->t('The "User timeline" type requires entering a timeline list.'));
}
return $entity;
}
public function save(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
$id = $entity
->get('id')->value;
$status = parent::save($form, $form_state);
Cache::invalidateTags([
'twitter_widget:' . $id,
'twitter_widget_view',
]);
switch ($status) {
case SAVED_NEW:
drupal_set_message($this
->t('Created the %label Twitter widget.', [
'%label' => $entity
->label(),
]));
break;
default:
drupal_set_message($this
->t('Saved the %label Twitter widget.', [
'%label' => $entity
->label(),
]));
}
$form_state
->setRedirect('entity.twitter_widget.canonical', [
'twitter_widget' => $entity
->id(),
]);
}
}