简体   繁体   中英

Drupal 7 module development. Showing specific class on specified time only

I'm conceptualizing a Drupal 7 custom module and I have a logic to show a class (with an image of red carpet) only at morning, then at noon (around 6pm) the class image will show another image (like a blue carpet).

What particular Drupal 7 hooks should I use for this module and properties? The only hooks Iknow to use is hook_menu and hook_block but Im still thinking how what exact hooks should be use..

Thank you. I a week young in module development :-)

I would use another approach than @shanet's one with a preprocess hook with the main idea to add a class somewhere in the page then use a css background image: "hook_preprocess_HOOK". More details at https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess_HOOK/7

In "hook_preprocess_HOOK", "hook" could be the name of your theme or module (prefered since your feature does not seem to be theme dependant) and "HOOK" could be "html", "page", "field", "node" etc ...

Inside the hook, you will find a "$variables['classes_array']" array of classes. Add the condition and class as needed.

Example :

function yourmodule_preprocess_html(&$variables) {
  $variables['classes_array'][] = isMorning() ? 'morning' : 'not-morning';
}

function isMorning() {
 ... your logic to check if it is morning or not ...
 ... returns a boolean ...
}

With this example you will get the class "morning" or "not-morning" in the body tag and could target anything with CSS according to your "morning" condition.

Assuming the content you want to display is a node there are two approaches you can take.

First is the Drupal 6 way, but it still works in Drupal 7. You can define a new content type with hook_node_info and then define an edit form with hook_form and view content with hook_view . Since this is the "old" way of doing things I won't elaborate more on it.

The fancy new way in Drupal 7 is through the Fields API. A field is able to be added to any content type and works in conjunction with other fields making it more flexible than the old D6 method of creating a new content type that I outlined above.

A field is comprised of two major parts: a widget and a formatter (but fields can have multiple widgets and formatters to allow the user to chose). A widget is used for inputting data into the field and will be displayed on a node's edit form. A formatter is used for displaying the data for the field to the user when viewing the node.

  1. Declare your field with hook_field_info .
  2. Use hook_field_widget_info to declare a widget for the field you defined in hook_field_info .
  3. Return a form from hook_field_widget_form which will be used on the edit form.
  4. Use hook_field_formatter_info to declare a formatter for the field you defined in hook_field_info .
  5. Return content to display from hook_field_formatter_view which will be displayed when viewing the node.

A full example for this would be a little lengthy for an answer here. Look up examples for those hooks and their documentation and you'll figure it out.

That's the basic Drupal fields flow at least. Sometimes it doesn't make sense to have a widget for a field. In cases like that, I use dynamic fields provided by the Display Suite (ds) module . DS dynamic fields are considerably easier to get up and running. I'll explain them briefly because it seems like it might in your case (there's really no use for a widget if you just want to display one image or another based on the time of day).

For a DS dynamic field, enable the ds module and then implement hook_ds_fields_info with something along the lines of:

function example_ds_fields_info($entity_type) {
  $fields = array();

  $fields['node']['image'] = array(
    'title'      => t('Dynamic Field Name'),
    'field_type' => DS_FIELD_TYPE_FUNCTION,
    'function'   => 'example_image',
  );

  if(isset($fields[$entity_type])) {
    return array($entity_type => $fields[$entity_type]);
  }
}

function example_foo($field) {
  return (is_morning() ? 'image one' : 'image two');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM