public function AvatarKitEntityMappingForm::buildForm in Avatar Kit 8.2
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/ AvatarKitEntityMappingForm.php, line 78
Class
- AvatarKitEntityMappingForm
- Configure Avatar Kit entity maps.
Namespace
Drupal\avatars\FormCode
public function buildForm(array $form, FormStateInterface $form_state) : array {
$form = parent::buildForm($form, $form_state);
$avatar_field_types = [
'file',
'image',
];
$headers = [
'label' => $this
->t('Entity type'),
'weight' => $this
->t('Bundle'),
'field' => $this
->t('Field'),
'field_settings' => $this
->t('Field settings'),
];
$form['map'] = [
'#type' => 'table',
'#header' => $headers,
'#empty' => $this
->t('No entity types with suitable avatar field targets found.'),
'#default_value' => [],
];
$fieldMap = $this->entityFieldManager
->getFieldMap();
unset($fieldMap['avatars_avatar_cache']);
$fieldsOptions = [];
foreach ($fieldMap as $entityType => $fields) {
foreach ($fields as $fieldName => $fieldInfo) {
[
'type' => $type,
'bundles' => $bundles,
] = $fieldInfo;
if (!in_array($type, $avatar_field_types)) {
continue;
}
foreach ($bundles as $bundle) {
$key = $entityType . ':' . $bundle;
$fieldsOptions[$key][$fieldName] = $this
->t('@field_name (@field_type)', [
'@field_name' => $fieldName,
'@field_type' => $type,
]);
}
}
}
foreach ($fieldsOptions as $key => $options) {
[
$entityType,
$bundle,
] = explode(':', $key);
$id = $entityType . '.' . $bundle . '.default';
$entityMap = $this->entityMappingStorage
->load($id);
$entityMapFieldName = $entityMap ? $entityMap
->getFieldName() : NULL;
$row = [];
$row['entity_type']['#plain_text'] = $entityType;
$row['bundle']['#plain_text'] = $bundle;
$row['field'] = [
'#type' => 'select',
'#title' => $this
->t('Field'),
'#title_display' => 'invisible',
'#default_value' => $entityMapFieldName,
'#options' => $options,
'#empty_option' => $this
->t('- None -'),
];
$row['field_settings'] = [];
if ($entityMapFieldName) {
$fieldConfigId = $entityType . '.' . $bundle . '.' . $entityMapFieldName;
try {
$url = Url::fromRoute('entity.field_config.' . $entityType . '_field_edit_form')
->setRouteParameter('field_config', $fieldConfigId);
$row['field_settings']['link'] = [
'#type' => 'link',
'#title' => $this
->t('Settings'),
'#url' => $url,
];
} catch (RouteNotFoundException $exception) {
// When field_ui is not enabled.
}
}
$form['map'][$key] = $row;
}
return $form;
}