You are here

class FacebookAlbumForm in Facebook Album 8

Configure facebook_album settings for this site.

Hierarchy

Expanded class hierarchy of FacebookAlbumForm

1 string reference to 'FacebookAlbumForm'
facebook_album.routing.yml in ./facebook_album.routing.yml
facebook_album.routing.yml

File

src/Form/FacebookAlbumForm.php, line 19
Contains \Drupal\facebook_album\Form\FacebookAlbumForm.

Namespace

Drupal\facebook_album\Form
View source
class FacebookAlbumForm extends ConfigFormBase {

  /**
   * The Drupal configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The FB Album controller.
   *
   * @var FacebookAlbumInterface
   */
  protected $facebook_album;

  /**
   * {@inheritdoc}
   *
   * @param FacebookAlbumInterface $facebook_album
   *   The controls of facebook album.
   */
  public function __construct(ConfigFactoryInterface $config_factory, FacebookAlbumInterface $facebook_album) {
    parent::__construct($config_factory);
    $this->configFactory = $config_factory;
    $this->facebook_album = $facebook_album;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('facebook_album.controller'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'facebook_album_config_form';
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Get module configuration.
    $config = $this
      ->config('facebook_album.settings')
      ->get();

    // @TODO: Style this, add more detail
    if (!empty($config['access_token'])) {
      $form['notice'] = [
        '#markup' => $this
          ->t('You already have configured your application.'),
      ];
    }
    $form['app_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Facebook App ID'),
      '#default_value' => $config['app_id'],
      '#required' => TRUE,
      '#description' => $this
        ->t('The application ID specified in your Facebook App\'s dashboard page.'),
    ];
    $form['app_secret'] = [
      '#type' => 'password',
      '#title' => $this
        ->t('Facebook App Secret'),
      '#default_value' => $config['app_secret'],
      '#required' => TRUE,
      '#description' => $this
        ->t('The application secret specified in your Facebook App\'s dashboard page. This field remains blank for security purposes. If you have already saved your application secret, leave this field blank, unless you wish to update it.'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * Call Facebook and get an access token for the given app
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $app_id = $form_state
      ->getValue('app_id');
    $app_secret = $form_state
      ->getValue('app_secret');
    $auth_path = FACEBOOK_ALBUM_API_AUTH_PATH . 'access_token';
    $parameters = [
      'client_id' => $app_id,
      'client_secret' => $app_secret,
      'grant_type' => 'client_credentials',
    ];
    $response = $this->facebook_album
      ->get($auth_path, $parameters);
    if (isset($response['error'])) {
      $message = $this->facebook_album
        ->translate_error($response['error']['code'], $response['error']['message']);
      $form_state
        ->setErrorByName('app_secret', $message);
    }
    else {
      $form_state
        ->setValue('access_token', $response['access_token']);
    }
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $page_id = $form_state
      ->getValue('page_id');
    $app_id = $form_state
      ->getValue('app_id');
    $app_secret = $form_state
      ->getValue('app_secret');
    $access_token = $form_state
      ->getValue('access_token');

    // Get module configuration.
    $this
      ->config('facebook_album.settings')
      ->set('page_id', $page_id)
      ->set('app_id', $app_id)
      ->set('app_secret', $app_secret)
      ->set('access_token', $access_token)
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FacebookAlbumForm::$configFactory protected property The Drupal configuration factory. Overrides FormBase::$configFactory
FacebookAlbumForm::$facebook_album protected property The FB Album controller.
FacebookAlbumForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FacebookAlbumForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
FacebookAlbumForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FacebookAlbumForm::getFormID public function
FacebookAlbumForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FacebookAlbumForm::validateForm public function Call Facebook and get an access token for the given app Overrides FormBase::validateForm
FacebookAlbumForm::__construct public function Overrides ConfigFormBase::__construct
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. 1
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. Overrides UrlGeneratorTrait::redirect
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.
FormInterface::getFormId public function Returns a unique string identifying the form. 236
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.