Skip to content

Instantly share code, notes, and snippets.

@davereid
Last active December 11, 2015 18:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davereid/4639015 to your computer and use it in GitHub Desktop.
Save davereid/4639015 to your computer and use it in GitHub Desktop.
Entity operation automation
<?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);
}
}
}
@davereid
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment