You are here

function _social_follow_landing_page_entity_validate in Open Social 10.0.x

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \_social_follow_landing_page_entity_validate()
  2. 10.1.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \_social_follow_landing_page_entity_validate()
  3. 10.2.x modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module \_social_follow_landing_page_entity_validate()

Validate function that overrides the tagging field with new values.

1 string reference to '_social_follow_landing_page_entity_validate'
social_follow_landing_page_field_widget_entity_reference_paragraphs_form_alter in modules/social_features/social_follow_taxonomy/modules/social_follow_landing_page/social_follow_landing_page.module
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 262
Contains social_follow_landing_page.module.

Code

function _social_follow_landing_page_entity_validate($element, FormStateInterface $form_state) {
  $parents = $element['#parents'];
  if (!empty($parents[0]) && !empty($form_state
    ->getValue($parents[0]))) {
    $parent_value = $form_state
      ->getValue($parents[0]);
    $subform = FALSE;
    if (isset($parents[1]) && isset($parent_value[$parents[1]]['subform']['field_section_paragraph'][0]['subform']['tagging'])) {
      $subform =& $parent_value[$parents[1]]['subform']['field_section_paragraph'][0]['subform'];
    }
    elseif (isset($parents[1]) && isset($parent_value[$parents[1]]['subform']['tagging'])) {
      $subform =& $parent_value[$parents[1]]['subform'];
    }
    elseif (isset($parents[1]) && isset($parent_value[$parents[1]]['field_tags'][0]['subform']['tagging'])) {
      $subform =& $parent_value[$parents[1]]['field_tags'][0]['subform'];
    }
    if ($subform) {
      $tag_value = $subform['tagging'];

      // Get the tagging service.
      $tag_service = Drupal::getContainer()
        ->get('social_tagging.tag_service');

      // Get the main categories.
      $categories = $tag_service
        ->getCategories();

      // Init categories.
      $tagging_values = [];
      $counter = 0;

      // Loop over the categories.
      foreach ($categories as $category) {
        if (!empty($tag_value['social_tagging_' . social_tagging_to_machine_name($category)])) {
          foreach ($tag_value['social_tagging_' . social_tagging_to_machine_name($category)] as $selected) {
            $tagging_values[] = [
              'target_id' => $selected,
              '_weight' => (string) $counter,
            ];
            $counter++;
          }
        }
      }
      $subform['field_tag'] = $tagging_values;

      // Set the values in the field_tag field.
      $form_state
        ->setValue($parents[0], $parent_value);
    }
  }
}