You are here

public function MentionsTypeForm::buildForm in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  2. 8 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  3. 8.2 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  4. 8.3 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  5. 8.4 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  6. 8.5 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  7. 8.6 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  8. 8.7 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  9. 8.8 modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  10. 10.3.x modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  11. 10.1.x modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()
  12. 10.2.x modules/custom/mentions/src/Form/MentionsTypeForm.php \Drupal\mentions\Form\MentionsTypeForm::buildForm()

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 EntityForm::buildForm

File

modules/custom/mentions/src/Form/MentionsTypeForm.php, line 94

Class

MentionsTypeForm
Class MentionsTypeForm.

Namespace

Drupal\mentions\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $plugin_names = $this->mentionsManager
    ->getPluginNames();
  $entity = $this->entity;
  $inputsettings = $entity
    ->get('input');
  $entity_id = isset($entity) ? $entity
    ->id() : '';
  $all_entitytypes = array_keys($this->entityTypeRepository
    ->getEntityTypeLabels());
  $candidate_entitytypes = [];
  foreach ($all_entitytypes as $entity_type) {
    $entitytype_info = $this->entityTypeManager
      ->getDefinition($entity_type);
    $configentityclassname = ContentEntityType::class;
    $entitytype_type = get_class($entitytype_info);
    if ($entitytype_type == $configentityclassname) {
      $candidate_entitytypes[$entity_type] = $entitytype_info
        ->getLabel()
        ->getUntranslatedString();
      $candidate_entitytypefields[$entity_type][$entitytype_info
        ->getKey('id')] = $entitytype_info
        ->getKey('id');
      if ($entity_type === 'user') {
        $candidate_entitytypefields[$entity_type]['name'] = 'name';
      }
      else {
        $candidate_entitytypefields[$entity_type][$entitytype_info
          ->getKey('label')] = $entitytype_info
          ->getKey('label');
      }
    }
  }
  $config = $this
    ->config('mentions.mentions_type.' . $entity_id);
  $form['name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Name'),
    '#required' => TRUE,
    '#description' => $this
      ->t('The human-readable name of this mention type. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
    '#default_value' => $config
      ->get('name'),
  ];
  $form['mention_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Mention Type'),
    '#options' => $plugin_names,
    '#default_value' => $config
      ->get('mention_type'),
  ];
  $form['description'] = [
    '#type' => 'textarea',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('Describe this mention type.'),
    '#default_value' => $config
      ->get('description'),
  ];
  $form['input'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Input Settings'),
    '#tree' => TRUE,
  ];
  $form['input']['prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Prefix'),
    '#default_value' => $config
      ->get('input.prefix'),
    '#size' => 2,
  ];
  $form['input']['suffix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Suffix'),
    '#default_value' => $config
      ->get('input.suffix'),
    '#size' => 2,
  ];
  $entitytype_selection = $config
    ->get('input.entity_type');
  $form['input']['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Entity Type'),
    '#options' => $candidate_entitytypes,
    '#default_value' => $entitytype_selection,
    '#ajax' => [
      'callback' => [
        $this,
        'changeEntityTypeInForm',
      ],
      'wrapper' => 'edit-input-value-wrapper',
      'event' => 'change',
      'progress' => [
        'type' => 'throbber',
        'message' => $this
          ->t('Please wait...'),
      ],
    ],
  ];
  if (!isset($candidate_entitytypefields)) {
    $inputvalue_options = [];
  }
  elseif (isset($entitytype_selection)) {
    $inputvalue_options = $candidate_entitytypefields[$entitytype_selection];
  }
  else {
    $inputvalue_options = array_values($candidate_entitytypefields)[0];
  }
  $inputvalue_default_value = count($inputsettings) == 0 ? 0 : $inputsettings['inputvalue'];
  $form['input']['inputvalue'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Value'),
    '#options' => $inputvalue_options,
    '#default_value' => $inputvalue_default_value,
    '#prefix' => '<div id="edit-input-value-wrapper">',
    '#suffix ' => '</div>',
    '#validated' => 1,
  ];
  $form['output'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Output Settings'),
    '#tree' => TRUE,
  ];
  $form['output']['outputvalue'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Value'),
    '#description' => $this
      ->t('This field supports tokens.'),
    '#default_value' => $config
      ->get('output.outputvalue'),
  ];
  $form['output']['renderlink'] = [
    '#type' => 'checkbox',
    '#title' => 'Render as link',
    '#default_value' => $config
      ->get('output.renderlink'),
  ];
  $form['output']['renderlinktextbox'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Link'),
    '#description' => $this
      ->t('This field supports tokens.'),
    '#default_value' => $config
      ->get('output.renderlinktextbox'),
    '#states' => [
      'visible' => [
        ':input[name="output[renderlink]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['output']['tokens'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => 'all',
      '#show_restricted' => TRUE,
      '#theme_wrappers' => [
        'form_element',
      ],
    ];
  }
  return parent::buildForm($form, $form_state);
}