You are here

public function SmartTitleConfigForm::buildForm in Smart Title 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

modules/smart_title_ui/src/Form/SmartTitleConfigForm.php, line 93

Class

SmartTitleConfigForm
SmartTitleConfigForm.

Namespace

Drupal\smart_title_ui\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('smart_title.settings');
  $entity_type_definitions = $this->entityTypeManager
    ->getDefinitions();

  // Collecting content entity types which have canonical link template.
  $content_entity_type_filter = function (EntityTypeInterface $entity_type_definition) {
    return $entity_type_definition instanceof ContentEntityTypeInterface && $entity_type_definition
      ->entityClassImplements(FieldableEntityInterface::class) && $entity_type_definition
      ->get('field_ui_base_route');
  };
  $valid_content_entity_types = array_filter($entity_type_definitions, $content_entity_type_filter);
  $entity_bundles = [];
  foreach (array_keys($valid_content_entity_types) as $entity_type_id) {
    $label_key = $valid_content_entity_types[$entity_type_id]
      ->getKey('label');
    if ($label_key) {
      $base_field_definitions = $this->entityFieldManager
        ->getBaseFieldDefinitions($entity_type_id);
      if (!$base_field_definitions[$label_key]
        ->isDisplayConfigurable('view')) {
        $entity_bundles[$entity_type_id]['label'] = $valid_content_entity_types[$entity_type_id]
          ->getLabel();
        $entity_bundles[$entity_type_id]['bundles'] = $this->entityTypeBundleInfo
          ->getBundleInfo($entity_type_id);
      }
    }
  }
  $defaults = $config
    ->get('smart_title') ?: [];
  foreach ($entity_bundles as $type => $definitions) {
    $options = [];
    $default = [];
    foreach ($definitions['bundles'] as $key => $info) {
      $options["{$type}:{$key}"] = $info['label'];
      if (in_array("{$type}:{$key}", $defaults)) {
        $default["{$type}:{$key}"] = "{$type}:{$key}";
      }
    }
    $form[$type . '_bundles'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('Smart Title for @entity-type', [
        '@entity-type' => $definitions['label'],
      ]),
      '#options' => $options,
      '#default_value' => $default,
    ];
  }
  return parent::buildForm($form, $form_state);
}