You are here

public function Settings::buildForm in Avatar Kit 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/Settings.php, line 68

Class

Settings
Configure avatar kit settings.

Namespace

Drupal\avatars\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $config = $this
    ->config('avatars.settings');

  // Define table.
  $headers = [
    'label' => $this
      ->t('Avatar Generator'),
    'type' => $this
      ->t('Type'),
    'plugin' => $this
      ->t('Plugin'),
    'settings' => $this
      ->t('Settings'),
    'enabled' => [
      'data' => $this
        ->t('Enabled'),
      'class' => [
        'checkbox',
      ],
    ],
    'weight' => $this
      ->t('Weight'),
    'operations' => $this
      ->t('Operations'),
  ];
  $form['avatar_generators_help'] = [
    '#prefix' => '<p>',
    '#markup' => $this
      ->t('A list of avatar generators to try for each user in order of preference.'),
    '#suffix' => '</p>',
  ];
  $form['avatar_generators'] = [
    '#type' => 'table',
    '#header' => $headers,
    '#empty' => $this
      ->t('No avatar generators found.'),
    '#attributes' => [
      'id' => 'avatar-generators',
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'generator-weight',
      ],
    ],
  ];

  /** @var \Drupal\avatars\AvatarGeneratorInterface[] $instances */
  $instances = AvatarGenerator::loadMultiple();
  uasort($instances, '\\Drupal\\avatars\\Entity\\AvatarGenerator::sort');
  foreach ($instances as $instance) {
    $form['avatar_generators'][$instance
      ->id()] = [];
    $row =& $form['avatar_generators'][$instance
      ->id()];
    $row['#attributes']['class'][] = 'draggable';
    $definition = $instance
      ->getPlugin()
      ->getPluginDefinition();
    $row['label']['#markup'] = $instance
      ->label();
    $row['type']['#markup'] = $definition['dynamic'] ? $this
      ->t('Dynamic') : $this
      ->t('Static');
    $row['plugin']['#markup'] = $definition['label'];
    $row['settings'] = $instance
      ->getPlugin()
      ->settingsSummary();
    $row['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled'),
      '#title_display' => 'invisible',
      '#default_value' => $instance
        ->status(),
      '#wrapper_attributes' => [
        'class' => [
          'checkbox',
        ],
      ],
    ];
    $row['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight'),
      '#title_display' => 'invisible',
      '#default_value' => $instance
        ->getWeight(),
      '#attributes' => [
        'class' => [
          'generator-weight',
        ],
      ],
    ];
    $operations = [];
    if ($instance
      ->access('update')) {
      $operations['edit'] = [
        'title' => $this
          ->t('Edit'),
        'weight' => 10,
        'url' => $instance
          ->toUrl('edit-form'),
      ];
    }
    if ($instance
      ->access('delete')) {
      $operations['delete'] = [
        'title' => $this
          ->t('Delete'),
        'weight' => 100,
        'url' => $instance
          ->toUrl('delete-form'),
      ];
    }
    $row['operations'] = [
      '#type' => 'operations',
      '#links' => $operations,
    ];
  }
  $form['refresh_interval']['#tree'] = TRUE;
  $intervals = $config
    ->get('refresh_interval');
  $form['refresh_interval']['dynamic'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Dynamic lifetime'),
    '#description' => $this
      ->t('How long dynamic avatars are cached before allowing refresh.'),
    '#default_value' => $intervals['dynamic'],
    '#step' => 60,
    '#min' => 0,
    '#field_suffix' => $this
      ->t('seconds'),
  ];

  /*
   * Keep unused avatars on file system (will use up more disk space)
   * Avatars must be purged manually if you change this settings.
   * Expire unused static avatars (will use up more network bandwidth)
   * */
  $form['refresh_interval']['static'] = [
    '#type' => 'unlimited_number',
    '#title' => $this
      ->t('Static lifetime'),
    '#description' => $this
      ->t('How long static avatars are cached. Only applies to avatars which are not the users preference.'),
    '#default_value' => $intervals['static'] < 1 ? UnlimitedNumber::UNLIMITED : $intervals['static'],
    '#step' => 60,
    '#min' => 60,
    '#field_suffix' => $this
      ->t('seconds'),
    '#options' => [
      'unlimited' => $this
        ->t('Never delete'),
      'limited' => $this
        ->t('Delete after'),
    ],
  ];
  return $form;
}