You are here

public function ContributeSettingsForm::buildForm in Contribute 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/ContributeSettingsForm.php, line 69

Class

ContributeSettingsForm
Configure contribute settings for this site.

Namespace

Drupal\contribute\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  // Add the core AJAX library.
  $form['#attached']['library'][] = 'core/drupal.ajax';
  $config = $this
    ->config('contribute.settings');
  $form['#attributes'] = [
    'novalidate' => TRUE,
  ];
  $form['account'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Drupal.org Account'),
    '#states' => [
      'visible' => [
        ':input[name="disable"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['account']['account_type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Account type'),
    '#description' => $this
      ->t('Select the type of Drupal.org account that you use to contribute back to Drupal'),
    '#options' => [
      'user' => $this
        ->t('Individual user'),
      'organization' => $this
        ->t('Organization'),
    ],
    '#default_value' => $config
      ->get('account_type') ?: 'user',
    '#states' => [
      'required' => [
        ':input[name="disable"]' => [
          'checked' => FALSE,
        ],
      ],
    ],
  ];
  $form['account']['user_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Drupal.org user name'),
    '#description' => $this
      ->t('Enter your user name. <a href=":href">Create new user account</a>', [
      ':href' => 'https://register.drupal.org/user/register',
    ]),
    '#default_value' => $config
      ->get('account_type') === 'user' ? $config
      ->get('account_id') : '',
    '#autocomplete_route_name' => 'contribute.autocomplete',
    '#autocomplete_route_parameters' => [
      'account_type' => 'user',
    ],
    '#states' => [
      'required' => [
        ':input[name="account_type"]' => [
          'value' => 'user',
        ],
      ],
      'visible' => [
        ':input[name="account_type"]' => [
          'value' => 'user',
        ],
      ],
    ],
  ];
  $form['account']['organization_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Drupal.org organization name'),
    '#description' => $this
      ->t('Enter your organization\'s name. <a href=":href">Create new organization</a>', [
      ':href' => 'https://www.drupal.org/node/add/organization',
    ]),
    '#default_value' => $config
      ->get('account_type') === 'organization' ? $config
      ->get('account_id') : '',
    '#autocomplete_route_name' => 'contribute.autocomplete',
    '#autocomplete_route_parameters' => [
      'account_type' => 'organization',
    ],
    '#states' => [
      'required' => [
        ':input[name="account_type"]' => [
          'value' => 'organization',
        ],
      ],
      'visible' => [
        ':input[name="account_type"]' => [
          'value' => 'organization',
        ],
      ],
    ],
  ];
  $form['warning'] = [
    '#type' => 'container',
    '#markup' => '<strong>' . $this
      ->t('Are you sure that you want to hide contribution information?') . '</strong><br/>' . $this
      ->t('Contribute information will only be configurable from the <a href=":href">Extend</a> page.', [
      ':href' => Url::fromRoute('system.modules_list')
        ->toString(),
    ]),
    '#attributes' => [
      'class' => [
        'messages messages--warning',
      ],
    ],
    '#states' => [
      'visible' => [
        ':input[name="disable"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['disable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Do not display contribution information"),
    '#default_value' => !$config
      ->get('status'),
    '#return_value' => TRUE,
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
    '#button_type' => 'primary',
  ];
  $form['actions']['delete'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Clear'),
    '#attributes' => [
      'class' => [
        'button--danger',
      ],
    ],
  ];

  // Add Ajax wrapper and submit to the form.
  if ($this
    ->isModal()) {
    $form['#form_wrapper_id'] = $this
      ->getWrapperId();
    $form['#prefix'] = '<div id="' . $this
      ->getWrapperId() . '">';
    $form['#suffix'] = '</div>';
    $form['actions']['submit']['#ajax'] = [
      'callback' => '::submitAjaxForm',
      'event' => 'click',
    ];
  }
  return $form;
}