You are here

public function ConfigContentTypes::buildForm in Allow a content type only once (Only One) 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/ConfigContentTypes.php, line 76

Class

ConfigContentTypes
Class ConfigContentTypes.

Namespace

Drupal\onlyone\Form

Code

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

  // Getting the available content types.
  $available_content_types = $this->onlyone
    ->getAvailableContentTypesForPrint();

  // Getting the number of content types.
  $cant_available_content_types = count($available_content_types);
  if ($cant_available_content_types) {

    // The details form element with the available content types.
    $form['available_content_type'] = [
      '#type' => 'details',
      '#title' => $this
        ->t("Content types available to have Only One content"),
      '#open' => TRUE,
    ];

    // All the available content types.
    $form['available_content_type']['onlyone_node_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Configure these content types to have Only One content per language:'),
      '#options' => $available_content_types,
      '#default_value' => $this
        ->config('onlyone.settings')
        ->get('onlyone_node_types'),
      '#description' => $this
        ->t('The selected content types will allow Only One content per language.'),
    ];
  }

  // Getting the non-available content types.
  $not_available_content_types = $this->onlyone
    ->getNotAvailableContentTypesForPrint();

  // Getting the number of not availables content types.
  $cant_not_available_content_types = count($not_available_content_types);

  // If all the content types are available we don't need to show the element.
  if ($cant_not_available_content_types) {
    $collapsed = $cant_available_content_types ? FALSE : TRUE;

    // The details form element with the unavailable content types.
    $form['not_available_content_type'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Content types not available to have Only One content per language'),
      '#description' => $this
        ->t('Content types which have more than one content in at least one language:'),
      '#open' => $collapsed,
      '#attributes' => [
        'class' => [
          'details-description--not-available-content-types',
        ],
      ],
    ];

    // Showing all the not availables content types.
    foreach ($not_available_content_types as $key => $value) {
      $form['not_available_content_type'][$key] = [
        '#type' => 'item',
        '#markup' => $value,
      ];
    }

    // Attaching the css file.
    $form['#attached']['library'] = [
      'onlyone/admin_settings',
    ];
  }
  if (!$cant_available_content_types && !$cant_not_available_content_types) {
    $form['not_available_content_type'] = [
      '#markup' => $this
        ->t('There are not content types on this site, go to the <a href=":add-content-type">Add content type</a> page to create one.', [
        ':add-content-type' => Url::fromRoute('node.type_add')
          ->toString(),
      ]),
    ];
  }

  // Show the submit button if there is availables content types.
  if ($cant_available_content_types) {
    return parent::buildForm($form, $form_state);
  }
  else {
    $form = parent::buildForm($form, $form_state);
    unset($form['actions']);
    return $form;
  }
}