2

I need to create a field which contains the date an entity/node was created. I know some people achieve this by using a bit of code in their template, but I need this to be a real, accessible field I can use on the entity/node display, as well as in Views (sort, filter, etc). Additionally I would like a field for the last modified date.

How would I do this properly in Drupal?

We did create a bit of SQL that we ran in PHPMyAdmin (it was a one time script, we need to schedule it to be run daily), but I have a feeling I could be doing something in Drupal. Am I wrong?

2 Answers 2

1

You could use the Computed Field module to display the date on your page. Create a field and set the php code to display the date in readable format.

Call your field something like 'Date created' and enter code like the following under Computed Code (PHP):

$entity_field[0]['value'] =date("Y/m/d",$entity->created);

Do not store the value in the database. You do not need to derive the value for views as the created date is available to views directly.

The last modified date is available as well as $entity->changed.

As an alternative, you might have a look at the Node Display Fields module, which has several widgets to display the type of information you are looking for.

7
  • Looks like this will work great...but what is the built-in/default way to access the date created via Views? Commented May 27, 2013 at 19:56
  • When you are adding fields to your view, Content: Post date and Content: Updated date are both available as fields. They are also available for sorts and filters if you wish.
    – Triskelion
    Commented May 27, 2013 at 20:03
  • Hmmm...I don't see those. Is that possibly only available for Nodes? I'm working with Entities. Commented May 27, 2013 at 20:11
  • Not quite sure what you mean by "working with Entities". If you mean that you are displaying a rendered entity, then yes you need to derive the fields for the display. If you created your view as a Content view, and you are displaying fields, then you should see the fields listed. Regardless, even if you are using a rendered entity as display, the fields should still be available for sorting and filtering.
    – Triskelion
    Commented May 27, 2013 at 20:23
  • That must be the difference. This is not a view of Nodes (again, Entities, Drealty Listings to be specific). When I create a View, I do not use "Show: Content" (only for Nodes, as far as I can tell), rather it is "Show: Drealty Listings". Under "Show: Content" my Drealty Entities are not available. Does that make sense? Commented May 27, 2013 at 20:49
1

The information is already in your database, so unless there is no other option, I wouldn't recommend to duplicate the data and store it again in another field.

Fortunately, there is another option, but it involves creating a custom module. If you're not a php coder, this may be over your head.

Drupal has a hook, hook_field_extra_fields, which allows you to use data as a field even though it's not technically a field. Here is a tutorial how to use it: Adding fake Drupal 7 node fields with custom theme functions. It even uses your exact question as an example: taking the created date and rendering it as a field. With a bit of copy-paste and some adjustments, you should be able to do the same for the modified date. The tutorial also talks about theming, which is nice but not a necessity, so you can skip that part if you don't need it.

4
  • I'll check that out. I need this field to fit in a long list of other fields, so off the top it sounds like it won't work (how would I get it into that list of fields?). How would I use a solution like this in Views for sorting/filtering? Commented May 27, 2013 at 20:13
  • That's exactly what hook_field_extra_fields does: it allows you to define a "pseudo field" and makes it available on the Manage Display page, together with all the other fields. See Add stuff to a node and configure it like fields for another detailed explanation. Views supports basic filtering and sorting by date by default; you don't need this solution for that. If you need more advanced views support, install the date_views module from the Date project. Commented May 27, 2013 at 21:00
  • Will that work, even if I'm using an Entity (Drealty)? Drealty listings are not Nodes. Commented May 27, 2013 at 21:20
  • I can't answer that, it depends on the views support in Drealty. I'd say: just give it a try, and if it doesn't work, ask more specific questions here or in the Drealty issue queue. Commented May 27, 2013 at 21:41

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.