protected function BundleFormAlter::addGroupContent in Organic groups 8
Adds the "is group content?" checkbox and target settings elements.
1 call to BundleFormAlter::addGroupContent()
- BundleFormAlter::formAlter in og_ui/
src/ BundleFormAlter.php - This is a helper for og_ui_form_alter().
File
- og_ui/
src/ BundleFormAlter.php, line 135
Class
- BundleFormAlter
- Helper for og_ui_form_alter().
Namespace
Drupal\og_uiCode
protected function addGroupContent(array &$form, FormStateInterface $form_state) {
// Get the stored config from the default group audience field if it exists.
$field = FieldConfig::loadByName($this->entityTypeId, $this->bundle, OgGroupAudienceHelperInterface::DEFAULT_FIELD);
$handler_settings = $field ? $field
->getSetting('handler_settings') : [];
// Compile a list of group entity types and bundles.
$target_types = [];
$target_bundles = [];
foreach (Og::groupTypeManager()
->getGroupMap() as $entity_type => $bundles) {
$target_types[$entity_type] = \Drupal::entityTypeManager()
->getDefinition($entity_type)
->getLabel();
$bundle_info = \Drupal::service('entity_type.bundle.info')
->getBundleInfo($entity_type);
foreach ($bundles as $bundle) {
$target_bundles[$entity_type][$bundle] = $bundle_info[$bundle]['label'];
}
}
$form['og']['og_group_content_bundle'] = [
'#type' => 'checkbox',
'#title' => new TranslatableMarkup('Group content'),
'#default_value' => $this->bundle ? Og::isGroupContent($this->entityTypeId, $this->bundle) : FALSE,
'#description' => empty($target_bundles) ? new TranslatableMarkup('There are no group bundles defined.') : '',
];
if ($target_types) {
// If a group audience field already exists, use its value. Otherwise fall
// back to the first entity type that was returned.
reset($target_types);
$target_type_default = $field && !empty($field
->getSetting('target_type')) ? $field
->getSetting('target_type') : key($target_types);
// If the target type was set using AJAX, use that instead of the default.
$ajax_value = $form_state
->getValue('og_target_type');
$target_type_default = $ajax_value ? $ajax_value : $target_type_default;
$form['og']['og_target_type'] = [
'#type' => 'select',
'#title' => new TranslatableMarkup('Target type'),
'#options' => $target_types,
'#default_value' => $target_type_default,
'#description' => new TranslatableMarkup('The entity type that can be referenced through this field.'),
'#ajax' => [
'callback' => [
$this,
'ajaxCallback',
],
'wrapper' => 'og-settings-wrapper',
],
'#states' => [
'visible' => [
':input[name="og_group_content_bundle"]' => [
'checked' => TRUE,
],
],
],
];
// Get the bundles that are acting as group.
$form['og']['og_target_bundles'] = [
'#prefix' => '<div id="og-settings-wrapper">',
'#suffix' => '</div>',
'#type' => 'select',
'#title' => new TranslatableMarkup('Target bundles'),
'#options' => $target_bundles[$target_type_default],
'#default_value' => !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : NULL,
'#multiple' => TRUE,
'#description' => new TranslatableMarkup('The bundles of the entity type that can be referenced. Optional, leave empty for all bundles.'),
'#states' => [
'visible' => [
':input[name="og_group_content_bundle"]' => [
'checked' => TRUE,
],
],
],
];
$form['#validate'][] = [
get_class($this),
'validateTargetBundleElement',
];
}
else {
// Don't show the settings, as there might be multiple OG audience fields
// in the same bundle.
$form['og']['og_group_content_bundle']['#disabled'] = TRUE;
}
}