简体   繁体   English

Drupal 7模块开发。 仅在指定时间显示特定班级

[英]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). 我正在概念化Drupal 7自定义模块,并且我有一种逻辑,只在早上展示课程(带有红地毯的图像),然后在中午(下午6点左右)展示课程图像(例如蓝色地毯) 。

What particular Drupal 7 hooks should I use for this module and properties? 我应该为该模块和属性使用哪些特定的Drupal 7挂钩? The only hooks Iknow to use is hook_menu and hook_block but Im still thinking how what exact hooks should be use.. 我知道唯一可以使用的钩子是hook_menu和hook_block,但是我仍在思考应该使用什么确切的钩子。

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". 我将使用@shanet的另一种方法,该方法带有预处理钩子,其主要思想是在页面中的某处添加一个类,然后使用CSS背景图像:“ hook_preprocess_HOOK”。 More details at https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_preprocess_HOOK/7 有关更多详细信息, 参见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 ... 在“ hook_preprocess_HOOK”中,“ hook”可以是您的主题或模块的名称(由于您的功能似乎与主题无关,因此首选“ hook”),而“ HOOK”可以是“ html”,“ page”,“ field”,“ node” “等等...

Inside the hook, you will find a "$variables['classes_array']" array of classes. 在挂钩内,您将找到一个“ $ variables ['classes_array']”类数组。 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. 在此示例中,您将在body标签中获得“ morning”或“ not-morning”类,并可以根据您的“ morning”条件使用CSS定位任何对象。

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 . 首先是Drupal的6路,但它仍然工作在Drupal 7.你可以定义新的内容类型与hook_node_info ,然后定义与编辑表单hook_form和查看内容与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. Drupal 7中新奇的方式是通过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. 字段可以添加到任何内容类型,并且可以与其他字段一起使用,这使其比我上面概述的用于创建新内容类型的旧D6方法更加灵活。

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 . 使用hook_field_info声明您的字段。
  2. Use hook_field_widget_info to declare a widget for the field you defined in hook_field_info . 使用hook_field_widget_info为您在hook_field_info定义的字段声明一个小部件。
  3. Return a form from hook_field_widget_form which will be used on the edit form. hook_field_widget_form返回一个表单,该表单将用于编辑表单。
  4. Use hook_field_formatter_info to declare a formatter for the field you defined in hook_field_info . 使用hook_field_formatter_info来声明您在hook_field_info定义的字段的格式化程序。
  5. Return content to display from hook_field_formatter_view which will be displayed when viewing the node. hook_field_formatter_view返回要显示的内容,该内容将在查看节点时显示。

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. 至少这是基本的Drupal字段流。 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 . 在这种情况下,我将使用Display Suite(ds)模块提供的动态字段。 DS dynamic fields are considerably easier to get up and running. DS动态字段非常容易设置和运行。 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: 对于DS动态字段,启用ds模块,然后通过以下方式实现hook_ds_fields_info

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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