You are here

class TwitterAccountsForm in Tweet Feed 4.x

Same name and namespace in other branches
  1. 8.3 src/Form/TwitterAccountsForm.php \Drupal\tweet_feed\Form\TwitterAccountsForm

Form controller for Tweet entity edit forms.

Hierarchy

Expanded class hierarchy of TwitterAccountsForm

1 string reference to 'TwitterAccountsForm'
tweet_feed.routing.yml in ./tweet_feed.routing.yml
tweet_feed.routing.yml

File

src/Form/TwitterAccountsForm.php, line 14

Namespace

Drupal\tweet_feed\Form
View source
class TwitterAccountsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'tweet_feed.twitter_accounts',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'tweet_feed_twitter_accounts';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $account_machine_name = NULL) {
    $config = $this
      ->config('tweet_feed.twitter_accounts');

    // Set up our settings form for this particular account (new or update)
    if (!empty($account_machine_name)) {
      $accounts = $config
        ->get('accounts');
      $account = $accounts[$account_machine_name];
      $form['account_machine_name'] = [
        '#type' => 'hidden',
        '#value' => $account_machine_name,
      ];
      $form['account_update'] = [
        '#type' => 'hidden',
        '#value' => 1,
      ];
      $account_name = $account['account_name'];
      $consumer_key = $account['consumer_key'];
      $consumer_secret = $account['consumer_secret'];
      $oauth_token = $account['oauth_token'];
      $oauth_token_secret = $account['oauth_token_secret'];
      $account_name_disabled = TRUE;
    }
    else {
      $account_name = $consumer_key = $oauth_token = NULL;
      $consumer_secret = $oauth_token_secret = NULL;
      $account_name_disabled = FALSE;
    }
    $form['tweet_feed_account'] = array(
      '#type' => 'fieldset',
      '#title' => t('Tweet Feed Twitter API Account Information Form'),
      '#description' => t('Provide information about the Twitter API account you wish to add. These can be used to get the feeds for any of our configurable options.'),
    );
    $form['tweet_feed_account']['account_name'] = array(
      '#type' => 'textfield',
      '#title' => t('Account Name'),
      '#max_length' => 128,
      '#disabled' => $account_name_disabled,
      '#required' => TRUE,
      '#default_value' => $account_name,
    );
    $form['tweet_feed_account']['consumer_key'] = array(
      '#type' => 'textfield',
      '#title' => t('Consumer Key'),
      '#max_length' => 255,
      '#required' => TRUE,
      '#default_value' => $consumer_key,
    );
    $form['tweet_feed_account']['consumer_secret'] = array(
      '#type' => 'textfield',
      '#title' => t('Consumer Secret'),
      '#max_length' => 255,
      '#required' => TRUE,
      '#default_value' => $consumer_secret,
    );
    $form['tweet_feed_account']['oauth_token'] = array(
      '#type' => 'textfield',
      '#title' => t('Oauth Token'),
      '#max_length' => 255,
      '#required' => TRUE,
      '#default_value' => $oauth_token,
    );
    $form['tweet_feed_account']['oauth_token_secret'] = array(
      '#type' => 'textfield',
      '#title' => t('Oauth Token Secret'),
      '#max_length' => 255,
      '#required' => TRUE,
      '#default_value' => $oauth_token_secret,
    );
    $form = parent::buildForm($form, $form_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $config = $this
      ->config('tweet_feed.twitter_accounts');
    $accounts = $config
      ->get('accounts');
    $values = $form_state
      ->getValues();

    // If we're updating, then do things differently
    if (empty($values['account_update'])) {
      $account_machine_name = preg_replace('/[^a-z0-9]+/', '_', strtolower($values['account_name']));
      if (!empty($accounts[$account_machine_name])) {
        $suffix = 1;
        do {
          $new_account_machine_name = $account_machine_name . '_' . $suffix;
          $suffix++;
        } while (!empty($accounts[$new_account_machine_name]));
        $account_machine_name = $new_account_machine_name;
      }
      if (empty($accounts[$account_machine_name])) {
        $accounts[$account_machine_name] = [];
      }
      else {
        $account_machine_name = $values['account_machine_name'];
      }
    }
    else {
      $account_machine_name = $values['account_machine_name'];
    }
    $accounts[$account_machine_name]['account_name'] = $values['account_name'];
    $accounts[$account_machine_name]['consumer_key'] = $values['consumer_key'];
    $accounts[$account_machine_name]['consumer_secret'] = $values['consumer_secret'];
    $accounts[$account_machine_name]['oauth_token'] = $values['oauth_token'];
    $accounts[$account_machine_name]['oauth_token_secret'] = $values['oauth_token_secret'];
    $this
      ->config('tweet_feed.twitter_accounts')
      ->set('accounts', $accounts)
      ->save();
    $url = Url::fromRoute('tweet_feed.twitter_accounts');
    $form_state
      ->setRedirectUrl($url);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 18
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 16
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.
TwitterAccountsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
TwitterAccountsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
TwitterAccountsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
TwitterAccountsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
TwitterAccountsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm