You are here

UserMenuAvatarConfigurationForm.php in User Menu Avatar (User Image in Menu) 8

File

src/form/UserMenuAvatarConfigurationForm.php
View source
<?php

/**
 * Drupal\user_menu_avatar\Form\UserMenuAvatarConfigurationForm.
 */
namespace Drupal\user_menu_avatar\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Defines our form class.
 */
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);
  }

}

Classes

Namesort descending Description
UserMenuAvatarConfigurationForm Defines our form class.