public function DomainEntitySettings::buildForm in Domain Access Entity 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/ DomainEntitySettings.php, line 132
Class
- DomainEntitySettings
- Provides a form to configure domain fields mappings.
Namespace
Drupal\domain_entity\FormCode
public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id, FALSE);
$this->entityType = $entity_type;
$bundles = $this
->getBundles($entity_type_id);
if (empty($bundles)) {
$form['info_no_bundles'] = [
'#markup' => $this
->t('Entity @entity_label (@entity_name) has not bundles yet.', [
'@entity_label' => $entity_type
->getLabel(),
'@entity_name' => $entity_type_id,
]),
];
// Early return when no bundles exists.
return $form;
}
// Populate domain list.
$domain_entity_options = $this->domainLoader
->loadOptionsList();
$domain_entity_behavior = [
DomainEntityMapper::BEHAVIOR_AUTO => $this
->t('Affiliate automatically created entity to a value (no widget on entity creation form, auto-assignation)'),
DomainEntityMapper::BEHAVIOR_USER => $this
->t('User choose affiliate, with a default value (form widget on the entity creation form)'),
];
$has_fields = FALSE;
foreach ($bundles as $bundle_id => $bundle) {
$bundle_label = $bundle['label'];
$settings = [];
if ($field = $this->mapper
->loadField($entity_type_id, $bundle_id)) {
$settings = $field
->getThirdPartySettings('domain_entity');
$has_fields = TRUE;
}
$settings += [
'domains' => [],
'behavior' => DomainEntityMapper::BEHAVIOR_AUTO,
];
$form[$bundle_id] = [
'#type' => 'details',
'#title' => $bundle_label,
'#open' => !empty($field),
];
$form[$bundle_id][$bundle_id . '_enable'] = [
'#title' => $this
->t('Enable domain entity access'),
'#type' => 'checkbox',
'#default_value' => !empty($field),
];
$states = [
'visible' => [
"input[name=\"{$bundle_id}_enable\"]" => [
'checked' => TRUE,
],
],
];
$form[$bundle_id][$bundle_id . '_behavior'] = [
'#title' => $this
->t("Choose which behavior must be used with the bundle @bundle_label ('@bundle_name')", [
'@bundle_label' => $bundle_label,
'@bundle_name' => $bundle_id,
]),
'#type' => 'select',
'#options' => $domain_entity_behavior,
'#default_value' => $settings['behavior'],
'#states' => $states,
];
$form[$bundle_id][$bundle_id . '_domains'] = [
'#title' => $this
->t("default domain value(s) for the bundle:", [
'@bundle_label' => $bundle_label,
'@bundle_name' => $bundle_id,
]),
'#type' => 'checkboxes',
'#options' => $domain_entity_options,
'#default_value' => $settings['domains'],
'#description' => $this
->t('When no domains selected entity is available for all domains'),
'#states' => $states,
];
}
// Check if content of this type exist in DB, if so prompt a warning.
$query = $this->entityTypeManager
->getStorage($entity_type_id)
->getQuery();
if ($has_fields && $query
->accessCheck(FALSE)
->range(0, 1)
->count()
->execute()) {
$form['message'] = [
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'class' => [
'messages',
'messages--warning',
],
],
],
],
'#markup' => $this
->t('* Beware you have entities of this type in your database, all unassigned entities will be assigned to the choosen default domain value(s), if you select "current domain" the unassigned entities will be assigned to the current domain. You can change the default value afterway without altering the existing entities domain value(s).'),
];
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save configuration'),
'#button_type' => 'primary',
];
return $form;
}