<?php

/**
 * @file
 * Allows to clone entity types from a simple link.
 */
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;

/**
 * Alter entity operations.
 *
 * @param array $operations
 *   Operations array as returned by
 *   \Drupal\Core\Entity\EntityStorageControllerInterface::getOperations().
 * @param \Drupal\Core\Entity\EntityInterface $entity
 *   The entity on which the linked operations will be performed.
 */
function entity_type_clone_entity_operation_alter(array &$operations, EntityInterface $entity) {
//Get the entity info.
  $info = $entity->getEntityType();
//Get the entity bundle.
  $bundle_of = $info->getBundleOf();
//Get the user account.
  $account = \Drupal::currentUser();
//Add the clone link to operations.
  $clone_types = ['node', 'paragraph', 'taxonomy_term', 'profile'];
  if ($account->hasPermission('access entity type clone') && (in_array($bundle_of, $clone_types))) {
    $operations['clone'] = [
      'title' => t('Clone @label', ['@label' => $entity->label()]),
      'weight' => 30,
      'url' => Url::fromRoute(
        "entity_type_clone.type", ['entity' => $bundle_of, 'bundle' => $entity->id(),
      ]),
    ];
  }
}
