public
Last active

Entity operation automation

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
<?php
 
function entity_get_operation_links($entity_type, $entity, $base_path = NULL) {
$build = array(
'#theme' => 'links__operations__' . $entity_type,
'#links' => array(),
'#attributes' => array('class' => array('links inline')),
);
 
list($entity_id) = entity_extract_ids($entity_type, $entity);
 
// Attempt to extract the base path from the entity URI.
if (!isset($base_path)) {
$uri = entity_uri($entity_type, $entity);
if (empty($uri['path'])) {
return array();
}
$base_path = preg_replace('/\/' . preg_quote($entity_id) . '\b.*/', '', $uri['path']);
}
 
$items = menu_contextual_links($entity_type, $base_path, array($entity_id));
$links = array();
foreach ($items as $class => $item) {
$class = drupal_html_class($class);
$links[$class] = array(
'title' => $item['title'],
'href' => $item['href'],
);
$item['localized_options'] += array('query' => array());
$item['localized_options']['query'] += drupal_get_destination();
$links[$class] += $item['localized_options'];
}
$build['#links'] = $links;
 
drupal_alter('contextual_links_view', $build, $items);
 
if (empty($links)) {
$build['#printed'] = TRUE;
}
 
return $build;
}
 
class views_handler_field_view_entity_operations extends views_handler_field_entity {
 
function option_definition() {
$options = parent::option_definition();
return $options;
}
 
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
 
$form['alter']['path'] = array('#access' => FALSE);
$form['alter']['external'] = array('#access' => FALSE);
}
 
function render($values) {
if ($entity = $this->get_value($values)) {
$links = entity_get_operation_links($this->entity_type, $entity);
return drupal_render($links);
}
}
}

Functionality has been integrated with http://drupal.org/project/helper and EntityHelper

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.