You are here

function _social_path_manager_update_alias in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  2. 8.5 modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  3. 8.6 modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  4. 8.8 modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  5. 10.3.x modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  6. 10.0.x modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  7. 10.1.x modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()
  8. 10.2.x modules/custom/social_path_manager/social_path_manager.module \_social_path_manager_update_alias()

Create the aliases for the views of the group.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity that is the parent for the alias.

string $op: The operation that is being performed.

bool $bulk: Parameter to tell if the operation is coming from a bulk or not.

Throws

\Exception

4 calls to _social_path_manager_update_alias()
social_path_manager_entity_delete in modules/custom/social_path_manager/social_path_manager.module
Implements hook_entity_delete().
social_path_manager_entity_insert in modules/custom/social_path_manager/social_path_manager.module
Implements hook_entity_insert().
social_path_manager_entity_update in modules/custom/social_path_manager/social_path_manager.module
Implements hook_entity_update().
_social_path_manager_update_group_tab_aliases in modules/custom/social_path_manager/social_path_manager.module
Common batch processing callback for all operations.

File

modules/custom/social_path_manager/social_path_manager.module, line 215
The Social Path Manager module.

Code

function _social_path_manager_update_alias(EntityInterface $entity, $op, $bulk = FALSE) {
  if ($entity
    ->getEntityTypeId() === 'group') {
    switch ($op) {
      case 'all':
      case 'update':
      case 'create':

        /** @var \Drupal\Core\Path\AliasManager $pam */
        $pam = \Drupal::service('path.alias_manager');

        // If it's a bulk generate then get the alias by path.
        if ($bulk === TRUE) {
          $url = Url::fromRoute('entity.group.canonical', [
            'group' => $entity
              ->id(),
          ]);
          $url = $url
            ->getInternalPath();
          $path['alias'] = $pam
            ->getAliasByPath('/' . $url);
        }
        else {

          // New alias.
          $path = \Drupal::service('pathauto.generator')
            ->updateEntityAlias($entity, 'update');
        }

        // Check if the alias changed.
        // If yes, then change all the other views.
        if (!empty($path)) {
          foreach (_social_path_manager_group_tabs() as $route) {
            $suffix = _social_path_manager_get_path_suffix($entity, $route);

            // Get alias of the group tab.
            $grouptab_alias = $pam
              ->getAliasByPath('/group/' . $entity
              ->id() . '/' . $suffix);

            /** @var \Drupal\Core\Path\AliasStorage $pas */
            $pas = \Drupal::service('path.alias_storage');

            // Check of the alias is an alias or path.
            $is_alias = $pas
              ->aliasExists($grouptab_alias, 'und');

            // Create a new alias when it does not exist.
            if ($op === 'create' && $is_alias === FALSE) {

              // Insert the alias for the other views.
              $pas
                ->save('/group/' . $entity
                ->id() . '/' . $suffix, $path['alias'] . '/' . $suffix, 'und');
            }

            // Update alias by deleting the old one and creating a new one.
            if ($op === 'update' || $op === 'all') {
              \Drupal::service('pathauto.alias_storage_helper')
                ->deleteBySourcePrefix('/group/' . $entity
                ->id() . '/' . $suffix);
              $pas
                ->save('/group/' . $entity
                ->id() . '/' . $suffix, $path['alias'] . '/' . $suffix, 'und');
            }
          }

          // Clear cache of the group tag and rebuild routes.
          \Drupal::service('cache_tags.invalidator')
            ->invalidateTags([
            'group:' . $entity
              ->id(),
          ]);
          \Drupal::service('router.builder')
            ->rebuild();
        }
        break;
      case 'delete':

        // Delete all the aliases of the deleted group.
        $storage_helper = \Drupal::service('pathauto.alias_storage_helper');
        $storage_helper
          ->deleteBySourcePrefix('/group/' . $entity
          ->id());
        break;
    }
  }
}