function social_tagging_social_tagging_field_form_alter in Open Social 10.3.x
Same name and namespace in other branches
- 8.9 modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
- 10.0.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
- 10.1.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
- 10.2.x modules/social_features/social_tagging/social_tagging.module \social_tagging_social_tagging_field_form_alter()
Function do a changes related to social_tagging field.
Parameters
array $form: Form array.
\Drupal\Core\Form\FormStateInterface $form_state: Form State object.
\Drupal\Core\Entity\EntityInterface $form_entity: Entity object.
2 calls to social_tagging_social_tagging_field_form_alter()
- social_tagging_form_alter in modules/
social_features/ social_tagging/ social_tagging.module - Implements hook_form_alter().
- social_tagging_inline_entity_form_entity_form_alter in modules/
social_features/ social_tagging/ social_tagging.module - Implements hook_inline_entity_form_entity_form_alter().
File
- modules/
social_features/ social_tagging/ social_tagging.module, line 141 - Contains social_tagging.module.
Code
function social_tagging_social_tagging_field_form_alter(array &$form, FormStateInterface &$form_state, EntityInterface $form_entity) {
if (!empty($form['social_tagging'])) {
// Load tag config..
$tag_settings = \Drupal::getContainer()
->get('config.factory')
->getEditable('social_tagging.settings');
// Switch for the entity_type.
switch ($form_entity
->getEntityTypeId()) {
case 'node':
$type = $form_entity
->bundle();
$tag_enabled = $tag_settings
->get('tag_node_type_' . $type);
break;
case 'group':
case 'profile':
$type = $form_entity
->getEntityTypeId();
$tag_enabled = $tag_settings
->get('tag_type_' . $type);
break;
default:
$tag_enabled = FALSE;
break;
}
// Get the taggingservice.
$tag_service = Drupal::getContainer()
->get('social_tagging.tag_service');
// Check if tagging is turned on/off.
if (!$tag_service
->active() || !$tag_service
->hasContent() || $tag_enabled == FALSE) {
// Remove the field from the form.
$form['social_tagging']['#access'] = FALSE;
return;
}
if (isset($form['social_tagging'])) {
// Add an extra vertical tab.
$form['tagging'] = [
'#type' => 'fieldset',
'#title' => t('Tag content'),
'#description' => '',
'#group' => 'group_tagging',
'#open' => TRUE,
'#weight' => 50,
];
// We want to move the tagging field in new fieldset
// "Tags". Only when the theme settings are updated.
$use_social_content_forms = theme_get_setting('content_entity_form_style');
if ($use_social_content_forms === 'open_social') {
if (!isset($form['#fieldgroups']['group_social_tags'])) {
$group_tags = new stdClass();
$group_tags->children = [];
$group_tags->parent_name = '';
$group_tags->label = t('Tags');
$group_tags->weight = '1';
$group_tags->format_type = 'fieldset';
$group_tags->format_settings = [
'required_fields' => TRUE,
'id' => 'tags',
'classes' => 'card',
'label' => t('Tags'),
];
$group_tags->mode = 'default';
$group_tags->context = 'form';
$group_tags->entity_type = 'user';
$group_tags->group_name = 'group_social_tags';
$form['#fieldgroups']['group_social_tags'] = $group_tags;
}
$form['tagging']['#type'] = 'details';
$form['tagging']['#title'] = '';
$form['tagging']['#group'] = 'group_social_tags';
$form['#group_children']['tagging'] = 'group_social_tags';
}
// Add Tags in flexible groups if enabled.
if (\Drupal::moduleHandler()
->moduleExists('social_group_flexible_group') && isset($form['#fieldgroups']['group_additional_details']) && $use_social_content_forms === 'open_social') {
$form['tagging']['#type'] = 'details';
$form['tagging']['#title'] = t('Tags');
$form['#fieldgroups']['group_additional_details']->children[] = 'tagging';
$form['#group_children']['tagging'] = 'group_additional_details';
}
if ($tag_service
->allowSplit()) {
// Get the default value.
$default_value = _social_tagging_node_form_defaults_values($form_entity);
// Get the main categories.
$categories = $tag_service
->getCategories();
// Loop over the categories.
foreach ($categories as $tid => $category) {
$field_name = 'social_tagging_' . social_tagging_to_machine_name($category);
// Get the corresponding items.
$options = $tag_service
->getChildren($tid);
// Display parent item in the tags list.
if ($tag_service
->useCategoryParent()) {
$options = [
$tid => $category,
] + $options;
}
// Only add a field if the category has any options.
if (count($options) > 0) {
// Add a field.
$form['tagging'][$field_name] = [
'#type' => 'select2',
'#title' => $category,
'#multiple' => TRUE,
'#default_value' => $default_value,
'#options' => $options,
'#group' => 'group_tagging',
];
}
}
// Deny access the social_tagging field altogether.
$form['social_tagging']['#access'] = FALSE;
// Add a custom submithandler.
$form['#validate'][] = '_social_tagging_entity_validate';
}
else {
$options = [];
foreach ($tag_service
->getCategories() as $key => $value) {
$options[$value] = $tag_service
->getChildren($key);
}
$form['social_tagging']['widget']['#options'] = $options;
// Move the social_tagging field in the group.
$form['tagging']['social_tagging'] = $form['social_tagging'];
unset($form['social_tagging']);
$form['tagging']['social_tagging']['#group'] = 'group_tagging';
}
if ($form_entity
->getEntityTypeId() === 'profile') {
if (isset($form['#fieldgroups']['group_tags'])) {
$form['tagging']['#type'] = 'container';
$form['tagging']['#group'] = 'group_tags';
}
else {
$form['tagging']['#title'] = t('Tags');
}
}
}
}
}