You are here

function mentions_insert_or_update in Open Social 8.2

Same name and namespace in other branches
  1. 8.9 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  2. 8 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  3. 8.3 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  4. 8.4 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  5. 8.5 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  6. 8.6 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  7. 8.7 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  8. 8.8 modules/custom/mentions/mentions.module \mentions_insert_or_update()
  9. 10.3.x modules/custom/mentions/mentions.module \mentions_insert_or_update()
  10. 10.0.x modules/custom/mentions/mentions.module \mentions_insert_or_update()
  11. 10.1.x modules/custom/mentions/mentions.module \mentions_insert_or_update()
  12. 10.2.x modules/custom/mentions/mentions.module \mentions_insert_or_update()

Handling mentions crud operations.

2 calls to mentions_insert_or_update()
mentions_entity_insert in modules/custom/mentions/mentions.module
Implements hook_entity_insert().
mentions_entity_update in modules/custom/mentions/mentions.module
Implements hook_entity_update().

File

modules/custom/mentions/mentions.module, line 66
Code for the mentions module.

Code

function mentions_insert_or_update(EntityInterface $entity, $update) {
  $config = \Drupal::config('mentions.settings');
  $supported_entities = $config
    ->get('supported_entity_types');
  if (empty($supported_entities) || !in_array($entity
    ->getEntityTypeId(), $supported_entities)) {
    return;
  }
  $type = $entity
    ->getEntityTypeId();
  $field_definitions = $entity
    ->getFieldDefinitions();
  $fields_with_text = [];
  $supported_field_types = [
    'text_with_summary',
    'text_long',
    'text',
  ];
  foreach ($field_definitions as $field_definition) {
    $field_type = $field_definition
      ->getType();
    if (isset($field_type) && in_array($field_type, $supported_field_types)) {
      array_push($fields_with_text, $field_definition
        ->getName());
    }
  }
  foreach ($fields_with_text as $text_field) {
    $entity_text_field = $entity
      ->get($text_field);
    $value = $entity_text_field->value;
    $format = !empty($entity_text_field->format) ? $entity_text_field->format : 'basic_html';
    $container = \Drupal::getContainer();
    $filter_mentions = MentionsFilter::create($container, [], 'filter_mentions', []);
    $filter_mentions
      ->setTextFormat($format);
    if ($filter_mentions
      ->shouldApplyFilter()) {
      $mentions = $filter_mentions
        ->getMentions($value);
      $auid = \Drupal::currentUser()
        ->id();
      mentions_crud_update($type, $mentions, $entity
        ->id(), $auid);
    }
  }
}