3

I'd like to create a new (date) field formatter in Views, but also keep the existing formatters intact. As of now, I've managed to use hook_field_views_data_alter() to entirely replace the exiting handler with mine. However I would prefer to have my custom formatter available in the dropdown below. Is it possible to append an option to this dropdown for my custom formatter?

views date formatter

2
  • If you have created a field formatter it should appear everywhere for the field, i.e. in "manage display" tab on the content type setting and in field options in views, etc. can you paste in the code you wrote for your field formatter?
    – AjitS
    Commented Jan 25, 2015 at 16:53
  • I did not create a field formatter... I assumed I had to do this with views field handlers. Will look into it, thanks! Commented Jan 25, 2015 at 17:49

1 Answer 1

3

If you wish to display a field in a different way, there are a different ways to go about it.

  1. Create a field template in your theme and change there. Just copying the field.tpl.php file would work changing the way the field is rendered on a node page. Note that field.tpl.php is a generic template which would be called on each field. You might want to try the specific template names like : field--article.tpl.php would get applied to all fields on article content type; field--body.tpl.php would get applied to body field on any content type; and field--body--article.tpl.php would only get applied to body field on the article content type.
  2. Overriding the theme_field() function. E.g. Implementing the following functions in your theme's template.php file would do the same as with template copying THEMENAME_field__body__article(), THEMENAME_field__article(), THEMENAME_field__body(), THEMENAME_field().
  3. If the field is rendered in a view you could override it with the view template. To find out the specific template which you would need to override, click on "Theme: information" under the "Advanced" settings in the view to find the field template that applies to your field.
  4. By creating your own custom field formatter by using this blog post. This will display the formatter in the dropdown as you intend.
  5. There is also a module which helps your create custom formatters, which are ctools exportable. Which means you could easily featurize them. More about the module:

    The Custom Formatters module allows users to easily create custom CCK/Field Formatters without the need to write a custom module. Custom Formatters can then be exported as CTools Exportables/Features or Drupal API Formatters.

1
  • I want to have a date formatter that displays dates in the form of "Today, tomorrow, this week, this month, etc. The relative date format from date module uses the smallest granularity which could be minutes or hours. Altering that output seems like a bad idea, so defining a new field formatter would be the best option for this case I guess. But good to see the other options as well! :) Commented Jan 25, 2015 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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