You are here

class UserMenuAvatarConfigurationForm in User Menu Avatar (User Image in Menu) 8

Same name and namespace in other branches
  1. 8.5 src/Form/UserMenuAvatarConfigurationForm.php \Drupal\user_menu_avatar\Form\UserMenuAvatarConfigurationForm
  2. 8.2 src/form/UserMenuAvatarConfigurationForm.php \Drupal\user_menu_avatar\Form\UserMenuAvatarConfigurationForm
  3. 8.3 src/Form/UserMenuAvatarConfigurationForm.php \Drupal\user_menu_avatar\Form\UserMenuAvatarConfigurationForm
  4. 8.4 src/Form/UserMenuAvatarConfigurationForm.php \Drupal\user_menu_avatar\Form\UserMenuAvatarConfigurationForm

Defines our form class.

Hierarchy

Expanded class hierarchy of UserMenuAvatarConfigurationForm

1 string reference to 'UserMenuAvatarConfigurationForm'
user_menu_avatar.routing.yml in ./user_menu_avatar.routing.yml
user_menu_avatar.routing.yml

File

src/form/UserMenuAvatarConfigurationForm.php, line 15

Namespace

Drupal\user_menu_avatar\Form
View source
class UserMenuAvatarConfigurationForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('user_menu_avatar.user_menu_avatar_settings');
    $avatar_shape_options = [
      'circle' => t('Circle'),
      'square' => t('Square'),
    ];
    $avatar_yes_no_options = [
      'yes' => t('Yes'),
      'no' => t('No'),
    ];
    $form['user_avatar_heading'] = [
      '#type' => 'item',
      '#markup' => t('<h2>Available User Menu Avatar Settings</h2>'),
    ];
    $form['avatar_shape_wrapper'] = [
      '#type' => 'fieldset',
      '#weight' => 0,
      '#attributes' => [
        'class' => [
          'avatar-shape',
        ],
      ],
    ];
    $form['avatar_shape_wrapper']['avatar_shape'] = [
      '#type' => 'radios',
      '#title' => t('User Menu Avatar Shape'),
      '#required' => TRUE,
      '#options' => $avatar_shape_options,
      '#description' => t('Choose the shape of the avatar image in the user menu.'),
      '#default_value' => $config
        ->get('avatar_shape') ?: 'circle',
    ];
    $form['avatar_size_wrapper'] = [
      '#type' => 'fieldset',
      '#weight' => 1,
      '#attributes' => [
        'class' => [
          'avatar-size',
        ],
      ],
    ];
    $form['avatar_size_wrapper']['avatar_size'] = [
      '#type' => 'textfield',
      '#attributes' => [
        ' type' => 'number',
      ],
      '#title' => t('User Menu Avatar Size (px)'),
      '#required' => TRUE,
      '#description' => t('The size of the User Menu Avatar in "pixels". Applies to both width and height. Numeric value only.'),
      '#maxlength' => 3,
      '#size' => 30,
      '#default_value' => $config
        ->get('avatar_size') ?: '50',
    ];
    $form['avatar_custom_image_field_wrapper'] = [
      '#type' => 'fieldset',
      '#weight' => 3,
      '#attributes' => [
        'class' => [
          'avatar-custom-image-field',
        ],
      ],
    ];
    $form['avatar_custom_image_field_wrapper']['avatar_custom_image_field'] = [
      '#type' => 'textfield',
      '#attributes' => [
        ' type' => 'text',
      ],
      '#title' => t('Custom Image Field Name (in place of, or to override, the user_picture field)'),
      '#required' => FALSE,
      '#description' => t('If you would like to use a custom field instead of the user_picture field, enter the field machine name here. *Field must be on the User Account'),
      '#maxlength' => 140,
      '#size' => 60,
      '#default_value' => $config
        ->get('avatar_custom_image_field' ?: ''),
    ];
    $form['avatar_show_picture_and_name_wrapper'] = [
      '#type' => 'fieldset',
      '#weight' => 4,
      '#attributes' => [
        'class' => [
          'avatar-show-picture-and-name',
        ],
      ],
    ];
    $form['avatar_show_picture_and_name_wrapper']['avatar_show_picture_and_name'] = [
      '#type' => 'radios',
      '#title' => t('Show Both Picture and Name'),
      '#required' => FALSE,
      '#options' => $avatar_yes_no_options,
      '#description' => t('Choose to show both the user_picture and the username.'),
      '#default_value' => $config
        ->get('avatar_show_picture_and_name') ?: 'no',
    ];
    $form['form_info'] = [
      '#type' => 'item',
      '#weight' => 10,
      '#markup' => t('<p>User Menu Avatar uses Background-image CSS to position the user_picture. The width and height are set by inline styles on the span element. The border radius only applies if you choose shape circle.</p>'),
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $this
      ->config('user_menu_avatar.user_menu_avatar_settings')
      ->set('avatar_shape', $values['avatar_shape']);
    $this
      ->config('user_menu_avatar.user_menu_avatar_settings')
      ->set('avatar_size', $values['avatar_size']);
    $this
      ->config('user_menu_avatar.user_menu_avatar_settings')
      ->set('avatar_custom_image_field', $values['avatar_custom_image_field']);
    $this
      ->config('user_menu_avatar.user_menu_avatar_settings')
      ->set('avatar_show_picture_and_name', $values['avatar_show_picture_and_name'])
      ->save();
    parent::submitForm($form, $form_state);
  }

}

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
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
UserMenuAvatarConfigurationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
UserMenuAvatarConfigurationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
UserMenuAvatarConfigurationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
UserMenuAvatarConfigurationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm