You are here

public function YoastSeoConfigForm::buildForm in Real-time SEO for Drupal 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 FormInterface::buildForm

File

src/Form/YoastSeoConfigForm.php, line 25

Class

YoastSeoConfigForm
Class YoastSeoConfigForm.

Namespace

Drupal\yoast_seo\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $yoast_seo_manager = \Drupal::service('yoast_seo.manager');

  // Available entity types supported by Yoast SEO.
  $entity_types = $this
    ->getAvailableEntityTypes();
  foreach ($entity_types as $entity_type => $entity_label) {

    // Get the available bundles Yoast SEO supports.
    $options = $yoast_seo_manager
      ->getAvailableBundles($entity_type);

    // Get the bundles Yoast SEO has been enabled for.
    $enabled_bundles = $yoast_seo_manager
      ->getEnabledBundles($entity_type);

    // Add a checkboxes collection to allow the administrator to
    // enable/disable Yoast SEO for supported bundles.
    $form[$entity_type] = array(
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('@label', array(
        '@label' => $entity_label,
      )),
      '#options' => $options,
      '#required' => FALSE,
      '#default_value' => $enabled_bundles,
    );
  }

  // Add a save action.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => $this
      ->t('Save'),
  );
  return $form;
}