You are here

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

Same name and namespace in other branches
  1. 8 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 11

Namespace

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('user_menu_avatar.settings');
    $avatar_shape_options = [
      'circle' => $this
        ->t('Circle'),
      'square' => $this
        ->t('Square'),
    ];
    $avatar_yes_no_options = [
      'yes' => $this
        ->t('Yes'),
      'no' => $this
        ->t('No'),
    ];
    $form['user_avatar_heading'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('<h2>Available User Menu Avatar Settings</h2>'),
      '#weight' => -10,
    ];
    $form['link_settings_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Link Settings'),
      '#attributes' => [
        'class' => [
          'link-settings-wrapper',
        ],
      ],
    ];
    $form['link_settings_wrapper']['link_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Set link Text'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Set the text of the menu link to be replaced.'),
      '#default_value' => $config
        ->get('link_text') ?: '',
    ];
    $form['logged_in_user_wraper'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Logged-in User Settings'),
      '#attributes' => [
        'class' => [
          'logged-in-user-wrapper',
        ],
      ],
    ];
    $form['logged_in_user_wraper']['show_menu_avatar'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Show Avatar'),
      '#required' => TRUE,
      '#options' => $avatar_yes_no_options,
      '#description' => $this
        ->t('Choose to show the user avatar.'),
      '#default_value' => $config
        ->get('show_menu_avatar') ?: 'no',
    ];
    $form['logged_in_user_wraper']['avatar_shape'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('User Menu Avatar Shape'),
      '#required' => TRUE,
      '#options' => $avatar_shape_options,
      '#description' => $this
        ->t('Choose the shape of the avatar.'),
      '#default_value' => $config
        ->get('avatar_shape') ?: 'circle',
      '#states' => [
        'visible' => [
          ':input[name="show_menu_avatar"]' => [
            'value' => 'yes',
          ],
        ],
      ],
    ];
    $form['logged_in_user_wraper']['avatar_size'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('User Menu Avatar Size (px)'),
      '#field_suffix' => 'px',
      '#required' => TRUE,
      '#description' => $this
        ->t('Set the size of the avatar in "pixels". Applies to both width and height. Numeric value only.'),
      '#maxlength' => 4,
      '#size' => 4,
      '#default_value' => $config
        ->get('avatar_size') ?: '50',
      '#states' => [
        'visible' => [
          ':input[name="show_menu_avatar"]' => [
            'value' => 'yes',
          ],
        ],
      ],
    ];
    $form['logged_in_user_wraper']['avatar_image_field'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Image field name'),
      '#required' => TRUE,
      '#description' => $this
        ->t('Set the field name to use for avatar. Default is "user_picture".'),
      '#maxlength' => 140,
      '#size' => 60,
      '#default_value' => $config
        ->get('avatar_image_field') ?: 'user_picture',
      '#states' => [
        'visible' => [
          ':input[name="show_menu_avatar"]' => [
            'value' => 'yes',
          ],
        ],
      ],
    ];
    $form['logged_in_user_wraper']['show_user_name'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Show User Name'),
      '#required' => TRUE,
      '#options' => $avatar_yes_no_options,
      '#description' => $this
        ->t('Choose to show the user name. Defaults to "displayName" value.'),
      '#default_value' => $config
        ->get('show_user_name') ?: 'no',
    ];
    $form['logged_in_user_wraper']['avatar_custom_name_field'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom name field name'),
      '#required' => FALSE,
      '#description' => $this
        ->t('Use a custom field for the user menu name. Leave blank to use default "displayName" value.'),
      '#maxlength' => 140,
      '#size' => 60,
      '#default_value' => $config
        ->get('avatar_custom_name_field') ?: '',
      '#states' => [
        'visible' => [
          ':input[name="show_user_name"]' => [
            'value' => 'yes',
          ],
        ],
      ],
    ];
    $form['anonymous_user_wrapper'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Anonymous User Settings'),
      '#attributes' => [
        'class' => [
          'anonymous-user-wrapper',
        ],
      ],
    ];
    $form['anonymous_user_wrapper']['show_anonymous_avatar'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Show Anonymous Avatar'),
      '#required' => TRUE,
      '#options' => $avatar_yes_no_options,
      '#description' => $this
        ->t('Choose to show the anonoymous user avatar.'),
      '#default_value' => $config
        ->get('show_anonymous_avatar') ?: 'no',
    ];
    $form['anonymous_user_wrapper']['anonymous_user_avatar'] = [
      '#type' => 'managed_file',
      '#title' => $this
        ->t('Anonymous User Avatar'),
      '#required' => FALSE,
      '#description' => $this
        ->t('Set an avatar for anonymous users.'),
      '#theme' => 'image_widget',
      '#preview_image_style' => 'medium',
      '#upload_validators' => [
        'file_validate_is_image' => [],
        'file_validate_extensions' => [
          'gif png jpg jpeg',
        ],
        'file_validate_size' => [
          25600000,
        ],
      ],
      '#upload_location' => 'public://user-menu-avatar/anonymous-avatar',
      '#default_value' => $config
        ->get('anonymous_user_avatar') ?: NULL,
    ];
    $form['anonymous_user_wrapper']['show_anonymous_name'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Show Anonymous Name'),
      '#required' => TRUE,
      '#options' => $avatar_yes_no_options,
      '#description' => $this
        ->t('Choose to show the anonoymous user name.'),
      '#default_value' => $config
        ->get('show_anonymous_name') ?: 'no',
    ];
    $form['anonymous_user_wrapper']['custom_anonymous_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Custom Anonymous Text'),
      '#required' => FALSE,
      '#description' => $this
        ->t('Set custom text to show for anonymous users. Leave blank to show "Anonymous".'),
      '#maxlength' => 255,
      '#size' => 60,
      '#default_value' => $config
        ->get('custom_anonymous_text') ?: '',
      '#states' => [
        'visible' => [
          ':input[name="show_anonymous_name"]' => [
            'value' => 'yes',
          ],
        ],
      ],
    ];
    $form['form_info'] = [
      '#type' => 'item',
      '#weight' => 10,
      '#markup' => $this
        ->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->configFactory
      ->getEditable('user_menu_avatar.settings')
      ->set('avatar_shape', $values['avatar_shape'])
      ->set('link_text', $values['link_text'])
      ->set('avatar_size', $values['avatar_size'])
      ->set('avatar_image_field', $values['avatar_image_field'])
      ->set('show_menu_avatar', $values['show_menu_avatar'])
      ->set('show_user_name', $values['show_user_name'])
      ->set('avatar_custom_name_field', $values['avatar_custom_name_field'])
      ->set('show_anonymous_avatar', $values['show_anonymous_avatar'])
      ->set('anonymous_user_avatar', $values['anonymous_user_avatar'])
      ->set('show_anonymous_name', $values['show_anonymous_name'])
      ->set('custom_anonymous_text', $values['custom_anonymous_text'])
      ->save();
    parent::submitForm($form, $form_state);

    // Flush render cache.
    \Drupal::service('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
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