You are here

class FblikebuttonFormSettings in Facebook Like Button 8

Hierarchy

Expanded class hierarchy of FblikebuttonFormSettings

1 string reference to 'FblikebuttonFormSettings'
fblikebutton.routing.yml in ./fblikebutton.routing.yml
fblikebutton.routing.yml

File

src/Form/FblikebuttonFormSettings.php, line 14

Namespace

Drupal\fblikebutton\Form
View source
class FblikebuttonFormSettings extends ConfigFormBase {

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

  /**
   * {@inheritdoc}
   */
  public function getEditableConfigNames() {
    return array(
      'fblikebutton.settings',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $fblikebutton_node_options = node_type_get_names();
    $config = $this
      ->config('fblikebutton.settings');
    $form['fblikebutton_dynamic_visibility'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Visibility settings'),
      '#open' => TRUE,
    );
    $form['fblikebutton_dynamic_visibility']['fblikebutton_node_types'] = array(
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Display the Like button on these content types:'),
      '#options' => $fblikebutton_node_options,
      '#default_value' => $config
        ->get('node_types'),
      '#description' => $this
        ->t('Each of these content types will have the "like" button automatically added to them.'),
    );

    /**
     * @TODO: Uncomment this when the module is also able to add the button to
     * the links area
     *
    $form['fblikebutton_dynamic_visibility']['fblikebutton_full_node_display'] = array(
      '#type' => 'radios',
      '#title' => $this->t('Where do you want to show the Like button (full node view)?'),
      '#options' => array(
        $this->t('Content area'),
        $this->t('Links area')
      ),
      '#default_value' => $config->get('full_node_display'),
      '#description' => $this->t('If <em>Content area</em> is selected, the button will appear in the same area as the node content. When you select <em>Links area</em> the Like button will be visible in the links area, usually at the bottom of the node (When you select this last option you may want to adjust the Appearance settings). You can also configure Static Like Button Blocks in'. \Drupal::l($this->t('block page'), Url::fromRoute('block.admin_display')) . '.'),
    );
    */
    $form['fblikebutton_dynamic_visibility']['fblikebutton_teaser_display'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Where do you want to show the Like button on teasers?'),
      '#options' => array(
        $this
          ->t('Don\'t show on teasers'),
        $this
          ->t('Content area'),
      ),
      '#default_value' => $config
        ->get('teaser_display'),
      '#description' => $this
        ->t('If you want to show the like button on teasers you can select the display area.'),
    );
    $form['fblikebutton_dynamic_appearance'] = array(
      '#type' => 'details',
      '#title' => $this
        ->t('Appearance settings'),
      '#open' => TRUE,
    );
    $form['fblikebutton_dynamic_appearance']['fblikebutton_layout'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Layout style'),
      '#options' => array(
        'standard' => $this
          ->t('Standard'),
        'box_count' => $this
          ->t('Box Count'),
        'button_count' => $this
          ->t('Button Count'),
        'button' => $this
          ->t('Button'),
      ),
      '#default_value' => $config
        ->get('layout'),
      '#description' => $this
        ->t('Determines the size and amount of social context next to the button.'),
    );

    // The actial values passed in from the options will be converted to a boolean
    // in the validation function, so it doesn't really matter what we use.
    $form['fblikebutton_dynamic_appearance']['fblikebutton_show_faces'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Show faces in the box?'),
      '#options' => array(
        t('Do not show faces'),
        $this
          ->t('Show faces'),
      ),
      '#default_value' => $config
        ->get('show_faces', TRUE),
      '#description' => $this
        ->t('Show profile pictures below the button. Only works if <em>Layout style</em> (found above) is set to <em>Standard</em> (otherwise, value is ignored).'),
    );
    $form['fblikebutton_dynamic_appearance']['fblikebutton_action'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Verb to display'),
      '#options' => array(
        'like' => $this
          ->t('Like'),
        'recommend' => $this
          ->t('Recommend'),
      ),
      '#default_value' => $config
        ->get('action'),
      '#description' => $this
        ->t('The verbiage to display inside the button itself.'),
    );
    $form['fblikebutton_dynamic_appearance']['fblikebutton_font'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Font'),
      '#options' => array(
        'arial' => 'Arial',
        'lucida+grande' => 'Lucida Grande',
        'segoe+ui' => 'Segoe UI',
        'tahoma' => 'Tahoma',
        'trebuchet+ms' => 'Trebuchet MS',
        'verdana' => 'Verdana',
      ),
      '#default_value' => $config
        ->get('font', 'arial'),
      '#description' => $this
        ->t('The font with which to display the text of the button.'),
    );
    $form['fblikebutton_dynamic_appearance']['fblikebutton_color_scheme'] = array(
      '#type' => 'select',
      '#title' => $this
        ->t('Color scheme'),
      '#options' => array(
        'light' => $this
          ->t('Light'),
        'dark' => $this
          ->t('Dark'),
      ),
      '#default_value' => $config
        ->get('color_scheme'),
      '#description' => $this
        ->t('The color scheme of the box environment.'),
    );
    $weight_description = "The weight determines where, at the content block, the like button will appear. The larger the weight, ";
    $weight_description .= "the lower it will appear on the node. For example, if you want the button to appear more toward the ";
    $weight_description .= "top of the node, choose <em>-40</em> as opposed to <em>-39, -38, 0, 1,</em> or <em>50,</em> etc. ";
    $weight_description .= "To position the Like button in its own block, go to the @link_to_block_page.";
    $form['fblikebutton_dynamic_appearance']['fblikebutton_weight'] = array(
      '#type' => 'number',
      '#title' => $this
        ->t('Weight'),
      '#default_value' => $config
        ->get('weight'),
      '#description' => $this
        ->t($weight_description, array(
        '@link_to_block_page' => Link::fromTextAndUrl($this
          ->t('block page'), Url::fromRoute('block.admin_display'))
          ->toString(),
      )),
    );
    $language_description = "Specific language to use. Default is English. Examples:<br />French (France): <em>fr_FR</em><br />";
    $language_description .= "French (Canada): <em>fr_CA</em><br />More information can be found at <a href='@info_url'>@info_url</a> ";
    $language_description .= "and a full XML list can be found at <a href='@list_url'>@list_url</a>";
    $form['fblikebutton_dynamic_appearance']['fblikebutton_language'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Language'),
      '#default_value' => $config
        ->get('language'),
      '#description' => $this
        ->t($language_description, array(
        '@info_url' => 'http://developers.facebook.com/docs/internationalization',
        '@list_url' => 'http://www.facebook.com/translations/FacebookLocales.xml',
      )),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (null != $form_state
      ->getValue('fblikebutton_weight')) {
      if (!is_numeric($form_state
        ->getValue('fblikebutton_weight'))) {
        $form_state
          ->setErrorByName('fblikebutton_bl_weight', $this
          ->t('The weight of the like button must be a number (examples: 50 or -42 or 0).'));
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $config = $this
      ->config('fblikebutton.settings');
    $node_types = $form_state
      ->getValue('fblikebutton_node_types');
    $full_node_display = $form_state
      ->getValue('fblikebutton_full_node_display');
    $teaser_display = $form_state
      ->getValue('fblikebutton_teaser_display');
    $layout = $form_state
      ->getValue('fblikebutton_layout');
    $show_faces = $form_state
      ->getValue('fblikebutton_show_faces');
    $action = $form_state
      ->getValue('fblikebutton_action');
    $font = $form_state
      ->getValue('fblikebutton_font');
    $color_scheme = $form_state
      ->getValue('fblikebutton_color_scheme');
    $weight = $form_state
      ->getValue('fblikebutton_weight');
    $language = $form_state
      ->getValue('fblikebutton_language');
    $config
      ->set('node_types', $node_types)
      ->set('full_node_display', $full_node_display)
      ->set('teaser_display', $teaser_display)
      ->set('layout', $layout)
      ->set('show_faces', $show_faces)
      ->set('action', $action)
      ->set('font', $font)
      ->set('color_scheme', $color_scheme)
      ->set('weight', $weight)
      ->set('language', $language)
      ->save();

    // Clear render cache
    $this
      ->clearCache();
  }

  /**
   * @TODO Clear render cache to make the button use the new configuration
   */
  protected function clearCache() {
    \Drupal::cache('render')
      ->invalidateAll();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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
FblikebuttonFormSettings::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
FblikebuttonFormSettings::clearCache protected function @TODO Clear render cache to make the button use the new configuration
FblikebuttonFormSettings::getEditableConfigNames public function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
FblikebuttonFormSettings::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
FblikebuttonFormSettings::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
FblikebuttonFormSettings::validateForm public function Form validation handler. Overrides FormBase::validateForm
FormBase::$configFactory protected property The config factory. 1
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.
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.