function social_follow_landing_page_field_widget_entity_reference_paragraphs_form_alter in Open Social 10.3.x
Same name and namespace in other branches
- 10.0.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \social_follow_landing_page_field_widget_entity_reference_paragraphs_form_alter()
- 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \social_follow_landing_page_field_widget_entity_reference_paragraphs_form_alter()
- 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \social_follow_landing_page_field_widget_entity_reference_paragraphs_form_alter()
Implements hook_field_widget_WIDGET_TYPE_form_alter().
File
- modules/
social_features/ social_follow_taxonomy/ modules/ social_follow_landing_page/ social_follow_landing_page.module, line 99 - Contains social_follow_landing_page.module.
Code
function social_follow_landing_page_field_widget_entity_reference_paragraphs_form_alter(&$element, FormStateInterface $form_state, $context) {
$subform = FALSE;
if (isset($element['subform']['field_tag'])) {
$subform =& $element['subform'];
}
elseif (isset($element['subform']['field_section_paragraph']['widget'])) {
$child_keys = Element::children($element['subform']['field_section_paragraph']['widget']);
$child_key = reset($child_keys);
if (isset($element['subform']['field_section_paragraph']['widget'][$child_key]['subform'])) {
$subform =& $element['subform']['field_section_paragraph']['widget'][$child_key]['subform'];
}
}
if ($subform) {
/** @var \Drupal\social_tagging\SocialTaggingService $tag_service */
$tag_service = Drupal::getContainer()
->get('social_tagging.tag_service');
// Check if tagging is turned on/off.
if (!$tag_service
->active() || !$tag_service
->hasContent()) {
// Remove the field from the form.
$subform['field_tag']['#access'] = FALSE;
return;
}
// Update view for landing pages.
if ($element['#paragraph_type'] === 'section') {
$subform['field_tag']['widget']['#title'] = t('Select a tag');
$subform['field_tag']['widget']['#multiple'] = FALSE;
$subform['field_tag']['widget']['#description'] = t('Selected tag will be displayed in a block with the options to follow this tag and see content relates to this tag.');
// Remove parent terms if they not allowed to follow.
if ($tag_service
->allowSplit() && !$tag_service
->useCategoryParent()) {
$tag_options =& $subform['field_tag']['widget']['#options'];
foreach ($tag_options as $tid => $tag_option) {
if ($tid === '_none') {
continue;
}
$terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadParents($tid);
if (empty($terms)) {
unset($tag_options[$tid]);
}
}
}
return;
}
// Add an extra vertical tab.
$subform['tagging'] = [
'#type' => 'fieldset',
'#title' => t('Tag content'),
'#description' => '',
'#group' => 'group_tagging',
'#open' => TRUE,
'#weight' => 50,
];
if ($tag_service
->allowSplit()) {
// Get the default value.
$default_value = [];
$form_object = $form_state
->getFormObject();
if ($form_object instanceof ContentEntityFormInterface) {
/** @var \Drupal\Core\Entity\EntityBase $form_entity */
$form_entity = $form_object
->getEntity();
// If the node exists, we need to get the default value.
if (($form_entity instanceof BlockContentInterface || $form_entity instanceof ContentEntityInterface) && $form_entity
->id() !== NULL) {
$parents = $subform['#parents'];
if (!empty($parents[0]) && $form_entity
->hasField($parents[0]) && !$form_entity
->get($parents[0])
->isEmpty()) {
$parent_value = $form_entity
->get($parents[0])
->getValue();
if (isset($parents[1])) {
$tag_value = NULL;
if (isset($parent_value[$parents[1]]['entity'])) {
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph_entity */
$paragraph_entity = $parent_value[$parents[1]]['entity'];
$tag_value = $paragraph_entity
->get('field_section_paragraph')->entity
->get('field_tag')
->getValue();
}
elseif (isset($parent_value[$parents[1]]['target_id'])) {
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph_entity */
$paragraph_entity = \Drupal::entityTypeManager()
->getStorage('paragraph')
->load($parent_value[$parents[1]]['target_id']);
$tag_value = $paragraph_entity
->get('field_tag')
->getValue();
}
if ($tag_value) {
foreach ($tag_value as $key => $value) {
if (isset($value['target_id'])) {
$default_value[] = $value['target_id'];
}
}
}
}
}
}
}
if (empty($default_value) && isset($context['items'])) {
$items_value = $context['items']
->getValue();
if (isset($items_value[0]['entity']) && $items_value[0]['entity'] instanceof ParagraphInterface) {
$paragraph_entity = $items_value[0]['entity'];
if ($paragraph_entity
->hasField('field_tag') && !$paragraph_entity
->get('field_tag')
->isEmpty()) {
$tag_value = $paragraph_entity
->get('field_tag')
->getValue();
if ($tag_value) {
foreach ($tag_value as $key => $value) {
if (isset($value['target_id'])) {
$default_value[] = $value['target_id'];
}
}
}
}
}
}
// 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);
// Only add a field if the category has any options.
if (count($options) > 0) {
// Add a field.
$subform['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.
$subform['field_tag']['#access'] = FALSE;
// Add a custom submithandler.
$element['#element_validate'][] = '_social_follow_landing_page_entity_validate';
}
else {
$options = [];
foreach ($tag_service
->getCategories() as $key => $value) {
$options[$value] = $tag_service
->getChildren($key);
}
$subform['field_tag']['widget']['#options'] = $options;
}
}
}