1

Setup

I am building a view that outputs human usable id's. These ids would look best if padded to 3 characters. For example the episodes of a tv show may look best if they are formated:

01-01 (Season 1 episode 1) 10-05 (Season 10 Episode 5) 03-22 (Season 03 Episode 22)

Problem

In my node I'm storing a simple integer (1-100). I want to keep the value as an integer not a string. How do I pad the integer with leading zeros on output.

Using simple php would be a simple str_pad($value, 2, '0', STR_PAD_LEFT); but I do not want to execute php in my views or even enable the php module/filter.

Solution

The solution must:

  • Use a core ability of views
  • Not require me to execute php stored in the database (not enable php module or anything equivalent)
  • Must allow me to maintain the field as an integer input/storage

I could probably write the views plugin for this but I was hoping their was already something available like a views integer format or something like that.

5
  • Welcome to Drupal Answers! Please add an explicit question in the question body, one that is not on the lines of "Any ideas?" The title doesn't count as explicit question.
    – apaderno
    Commented Jun 17, 2016 at 6:56
  • Read the title of the post. It is a concise clean question. Also i have 200 reputation on this site and more on other in network sites been using stack exchange for 5 years. Commented Jun 17, 2016 at 11:45
  • As I said, the question title doesn't count as asking a question. You aren't asking a question, but making a request.
    – apaderno
    Commented Jun 17, 2016 at 11:55
  • I don't think you know what a question is. A question is a request for information. A good question has a correct or best answer. My title is actually a yes or no question which is about as easy as it gets. A best answer would provide an option closest to my requirements and Drupal coding standards. You are free to not like my question but it meets all the requirements of the stack exchange network. If you still don't like it flag it and go away. Commented Jun 17, 2016 at 12:00
  • Since there isn't any explicit question, the question says I was hoping their was already something available like a views integer format or something like that. and you aren't accepting an answer suggesting how to achieve it with code, I take the question is a request for suggesting/finding a module implementing a plugin, and I will flag it for closing it.
    – apaderno
    Commented Jun 17, 2016 at 12:19

1 Answer 1

1

Go to /sites/all/themes/yourtheme/ folder and in template.php add the following code:

function yourtheme_preprocess_views_view_fields($vars){

  if ($vars['view']->name == "view_name" && $vars['view']->current_display == "block_1") {
    $vars['fields']['field_example']->content = str_pad($vars['fields']['field_example']->raw, 2, 0, STR_PAD_LEFT);
  }

}
  • replace yourtheme with the actual name of your theme
  • replace view_name with the name of your view.
  • replace block_1 with the machine name of your view.
  • replace example with the name of your field (2 replacements).

Lastly, in Drupal, clear/flush your cache to see the changes.

14
  • This does not use a module or views setting. And while this may be a reasonable use of theme preprocessor Im not a fan of overriding views in the theme layer. Better to do it in a view_post_query hook or something like that in a custom module. Still looking for a module. Commented Jun 17, 2016 at 3:51
  • @danielson317 you will end up with the same result...
    – No Sssweat
    Commented Jun 17, 2016 at 3:56
  • what is the hook to preprocess a view?
    – No Sssweat
    Commented Jun 17, 2016 at 3:57
  • 2
    Requests for module recommendations is something we don't do here @danielson317, it doesn't fit the Q+A format well. That's why you'll often find that people offer practical solutions here rather than linking off to a possibility elsewhere. As an aside, I think your requirements are at odds with what's physically possible here, insofar as your solution must Use a core ability of views. Core views doesn't have this ability, so currently you're simply out of luck. Yes writing a custom field handler is an option, but you'll have to do that as a "php code implementation" in a custom module, so..
    – Clive
    Commented Jun 17, 2016 at 8:30
  • 2
    ..that's ruled out too by the current requirements. It's hard to see how you're going to solve this unless you relax the rules here. A views pre-render hook would also need to be a "php code implementation" in a custom module, for example
    – Clive
    Commented Jun 17, 2016 at 8:31

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