2

now and then i use php in views. Now i do this only when it's the only option. I often use this for specific action links (action links are links like: edit delete).

Now i don't want to save php in my database, it doesn't belong there. So i was wondering if their was an other option to add those action links i generate with php to each row. The action links need data from the current row.

Thx Nuctorn

  • 4
    you can always modify any of the templates Views uses. Click on the theme link in the UI to see which one would serve the best purpose. – Jimajamma Apr 27 '14 at 15:11
  • 2
    There are a lot of different ways and the best way depends on what you need to achieve. Action links like edit for things like nodes are actually already available in views without needing to code anything. – rooby Apr 28 '14 at 11:06
  • @Jimajamma, i dont wan't to use the template files for it. The template files are for templating, not for manipulating the data. In drupal their are a lot of ways for doing things, but those ways are not always the "right" ones. – mgoubert Apr 28 '14 at 18:02
  • @nuctorn, I stand corrected and humbly apologize for my erroneous commentary. – Jimajamma Apr 29 '14 at 0:38
11

You definitely want to create a views handler with your custom php code. This is the standard way to do custom php stuff on views module.

Here you have an example:

mymodule.info

...
files[] = views/handlers/views_handler_field_node_link_custom_stuff.inc
...

mymodule.module

/**
 * Implements hook_views_api().
 */
function mymodule_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'MYMODULE') . '/views',
  );
}

In MYMODULE/views/mymodule.views.inc:

function mymodule_views_data() {
  $data['node']['custom_stuff'] = array(
    'field' => array(
      'title' => t('Custom link'),
      'help' => t('Provide a custom content link.'),
      'handler' => 'views_handler_field_node_custom_stuff',
    ),
  );
  return $data;
}

MYMODULE/views/handlers/'views_handler_field_node_link_custom_stuff.inc

/**
 * Field handler to present some custom HTML.
 */
class views_handler_field_node_custom_stuff extends views_handler_field_node {
  function construct() {
    parent::construct();
    $this->additional_fields['type'] = 'type';
  }
  function query() {
  $this->ensure_my_table();
  $this->add_additional_fields();
  }
  function render($values) {
    $type = $this->get_value($values, 'type');
    $nid = $this->get_value($values, 'nid');
    return mymodule_print_my_link($nid, $type);
  }
};
2

You can insert these links in the template layer. In the view's advanced settings, in Advanced->Theme, you can see what template names can there be. Then, copy the corresponding template file over into your theme layer and add the needed links.

1

You should definitely try Views built-in contextual links. Simply add Global: Contextual Links fields to your view. First you can add Content: Edit Link and Content: Delete Link and exclude them from display. And then you can plant the Global: Contextual Links field with via a simple checkbox. Be sure to add the edit and delete fields strictly before (on top of) your Global: contextual links field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.