quick_clone-8.x-1.x-dev/quick_clone.module
quick_clone.module
<?php
/**
* @file
* Contains quick_clone.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
/**
* Implements hook_help().
*/
function quick_clone_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the quick_clone module.
case 'help.page.quick_clone':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Module which allow cloning nodes with paragraphs fields') . '</p>';
return $output;
default:
}
}
/**
* Helping function.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Entity which will be linked for cloning.
*
* @return array
* An operations array as returned by
* EntityListBuilderInterface::getOperations().
*
* @see \Drupal\Core\Entity\EntityListBuilderInterface::getOperations()
*/
function quick_clone_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity instanceof Node) {
$operations['clone'] = [
'title' => t('Clone'),
'url' => Url::fromRoute('quick_clone', ['id' => $entity->id()]),
'weight' => 150,
];
}
return $operations;
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function quick_clone_menu_local_tasks_alter(&$data, $route_name) {
/** @var Drupal\node\Entity\Node $node */
$node = \Drupal::routeMatch()->getParameter('node');
if ($node && $node instanceof Node) {
if ((strpos($route_name, 'entity.node.edit_form') === 0) || (strpos($route_name, 'entity.node.canonical') === 0)) {
$data['tabs'][0]['entity.node.quick_clone'] = [
'#theme' => 'menu_local_task',
'#link' => [
'title' => t('Clone'),
'url' => Url::fromRoute('quick_clone', ['id' => $node->id()]),
],
];
}
}
}
