You are here

public function TwitterWidgetForm::buildForm in Twitter Profile Widget 8

Form constructor.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/TwitterWidgetForm.php, line 19

Class

TwitterWidgetForm
Form controller for Twitter widget edit forms.

Namespace

Drupal\twitter_profile_widget\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /* @var $entity \Drupal\twitter_profile_widget\Entity\TwitterWidget */
  $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');
  }

  // Conditionally display supplementary fields based on "type" selection.
  $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',
        ],
      ],
    ],
  ];

  // Place API call types into fieldset for better UX.
  $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;
}