I have pages in a view that I want to display during a season and there is a field that is a taxonomy of {summer, fall, winter, spring}. My problem is that I cannot figure out how to get the current season and compare it to the field in my pages. I can calculate the season using php:

$value = '';
$today = new DateTime();
echo 'Today is: ' . $today->format('m-d-Y') . '<br />';

// get the season dates
$spring = new DateTime('March 20');
$summer = new DateTime('June 20');
$fall = new DateTime('September 22');
$winter = new DateTime('December 21');

switch(true) {
    case $today >= $spring && $today < $summer:
        $value = 'spring';
        break;

    case $today >= $summer && $today < $fall:
       $value = 'summer';
        break;

    case $today >= $fall && $today < $winter:
        $value = 'autumn';
        break;

    default:
        $value = 'winter';!';
}

And I tried to put it into the computed_field module's field, but I can't figure out how to use it inside views. How can I access $value and require that the season field equal this value to display the page?

Comments

wombatbuddy’s picture

What actions will you do after you detect a season?

tjtj’s picture

I want the view to display the content tagged with the current season

wombatbuddy’s picture

I think, we can accomplish this with a custom module.
Share the name of your view.

wombatbuddy’s picture

Also, share the name of the field that contains season term.

tjtj’s picture

Thanks the the help

The view is names "pests" 

The field is called "seasons" 

Seasons is a list of up to 4 season strings, which are in a taxonomy called seasons, and are ctl-clicked from a dropdown list in contents of type "Pest of the month"

Thanks for the help

Here is the view:

uuid: d6d799c3-e082-40dd-8153-39128a2094f1
langcode: en
status: true
dependencies:
  config:
    - field.storage.node.body
    - field.storage.node.field_pest
    - field.storage.node.field_season
    - field.storage.node.field_this_season
    - image.style.media_library
    - node.type.pest_of_the_month
    - taxonomy.vocabulary.seasons
  content:
    - 'taxonomy_term:seasons:41111406-9b22-47d7-9724-f5ed695fbd36'
  module:
    - computed_field
    - image
    - node
    - taxonomy
    - text
    - user
    - views_infinite_scroll
id: pests
label: Pests
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
  default:
    display_plugin: default
    id: default
    display_title: Master
    position: 0
    display_options:
      access:
        type: perm
        options:
          perm: 'access content'
      cache:
        type: tag
        options: {  }
      query:
        type: views_query
        options:
          disable_sql_rewrite: false
          distinct: false
          replica: false
          query_comment: ''
          query_tags: {  }
      exposed_form:
        type: basic
        options:
          submit_button: Apply
          reset_button: false
          reset_button_label: Reset
          exposed_sorts_label: 'Sort by'
          expose_sort_order: true
          sort_asc_label: Asc
          sort_desc_label: Desc
      pager:
        type: infinite_scroll
        options:
          items_per_page: 10
          offset: 0
          id: 0
          total_pages: null
          tags:
            previous: '‹ Previous'
            next: 'Next ›'
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
          views_infinite_scroll:
            button_text: 'Load More'
            automatically_load_content: false
      style:
        type: grid
        options:
          grouping:
            -
              field: field_season
              rendered: true
              rendered_strip: false
          columns: 6
          automatic_width: true
          alignment: horizontal
          col_class_default: true
          col_class_custom: ''
          row_class_default: true
          row_class_custom: ''
      row:
        type: fields
        options:
          default_field_elements: true
          inline:
            title: title
            field_pest: field_pest
          separator: ' '
          hide_empty: false
      fields:
        title:
          id: title
          table: node_field_data
          field: title
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: false
            ellipsis: false
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: h3
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: string
          settings:
            link_to_entity: true
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          entity_type: node
          entity_field: title
          plugin_id: field
        field_pest:
          id: field_pest
          table: node__field_pest
          field: field_pest
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: false
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: target_id
          type: image
          settings:
            image_style: media_library
            image_link: content
          group_column: ''
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        body:
          id: body
          table: node__body
          field: body
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: text_summary_or_trimmed
          settings:
            trim_length: 1000
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        field_season:
          id: field_season
          table: node__field_season
          field: field_season
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: target_id
          type: entity_reference_label
          settings:
            link: true
          group_column: target_id
          group_columns: {  }
          group_rows: true
          delta_limit: 4
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
        field_this_season:
          id: field_this_season
          table: node__field_this_season
          field: field_this_season
          relationship: none
          group_type: group
          admin_label: ''
          label: ''
          exclude: true
          alter:
            alter_text: false
            text: ''
            make_link: false
            path: ''
            absolute: false
            external: false
            replace_spaces: false
            path_case: none
            trim_whitespace: false
            alt: ''
            rel: ''
            link_class: ''
            prefix: ''
            suffix: ''
            target: ''
            nl2br: false
            max_length: 0
            word_boundary: true
            ellipsis: true
            more_link: false
            more_link_text: ''
            more_link_path: ''
            strip_tags: false
            trim: false
            preserve_tags: ''
            html: false
          element_type: ''
          element_class: ''
          element_label_type: ''
          element_label_class: ''
          element_label_colon: false
          element_wrapper_type: ''
          element_wrapper_class: ''
          element_default_classes: true
          empty: ''
          hide_empty: false
          empty_zero: false
          hide_alter_empty: true
          click_sort_column: value
          type: computed_string
          settings:
            sanitized: 1
            cache_unit: '-1'
            cache_duration: '1'
          group_column: value
          group_columns: {  }
          group_rows: true
          delta_limit: 0
          delta_offset: 0
          delta_reversed: false
          delta_first_last: false
          multi_type: separator
          separator: ', '
          field_api_classes: false
          plugin_id: field
      filters:
        status:
          value: '1'
          table: node_field_data
          field: status
          plugin_id: boolean
          entity_type: node
          entity_field: status
          id: status
          expose:
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
          group: 1
        type:
          id: type
          table: node_field_data
          field: type
          value:
            pest_of_the_month: pest_of_the_month
          entity_type: node
          entity_field: type
          plugin_id: bundle
          expose:
            operator_limit_selection: false
            operator_list: {  }
        field_season_target_id:
          id: field_season_target_id
          table: node__field_season
          field: field_season_target_id
          relationship: none
          group_type: group
          admin_label: ''
          operator: or
          value:
            - 9
          group: 1
          exposed: false
          expose:
            operator_id: ''
            label: ''
            description: ''
            use_operator: false
            operator: ''
            operator_limit_selection: false
            operator_list: {  }
            identifier: ''
            required: false
            remember: false
            multiple: false
            remember_roles:
              authenticated: authenticated
            reduce: false
          is_grouped: false
          group_info:
            label: ''
            description: ''
            identifier: ''
            optional: true
            widget: select
            multiple: false
            remember: false
            default_group: All
            default_group_multiple: {  }
            group_items: {  }
          reduce_duplicates: false
          type: textfield
          limit: true
          vid: seasons
          hierarchy: false
          error_message: true
          plugin_id: taxonomy_index_tid
      sorts:
        created:
          id: created
          table: node_field_data
          field: created
          order: DESC
          entity_type: node
          entity_field: created
          plugin_id: date
          relationship: none
          group_type: group
          admin_label: ''
          exposed: false
          expose:
            label: ''
          granularity: second
      title: 'Pests that Brooks can control'
      header:
        area:
          id: area
          table: views
          field: area
          relationship: none
          group_type: group
          admin_label: ''
          empty: false
          tokenize: false
          content:
            value: '<h2>Pests of the season</h2>'
            format: full_html
          plugin_id: text
      footer: {  }
      empty: {  }
      relationships: {  }
      arguments: {  }
      display_extenders: {  }
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - user
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.body'
        - 'config:field.storage.node.field_pest'
        - 'config:field.storage.node.field_season'
        - 'config:field.storage.node.field_this_season'
  block_1:
    display_plugin: block
    id: block_1
    display_title: Block
    position: 2
    display_options:
      display_extenders: {  }
      style:
        type: grid
        options:
          grouping: {  }
          columns: 5
          automatic_width: true
          alignment: horizontal
          col_class_default: true
          col_class_custom: ''
          row_class_default: true
          row_class_custom: ''
      defaults:
        style: false
        row: false
        pager: false
        fields: true
      row:
        type: fields
        options:
          default_field_elements: true
          inline:
            field_pest: field_pest
            field_this_season: field_this_season
          separator: ''
          hide_empty: false
      pager:
        type: infinite_scroll
        options:
          items_per_page: 0
          offset: 0
          id: 0
          total_pages: null
          tags:
            previous: '‹ Previous'
            next: 'Next ›'
          expose:
            items_per_page: false
            items_per_page_label: 'Items per page'
            items_per_page_options: '5, 10, 25, 50'
            items_per_page_options_all: false
            items_per_page_options_all_label: '- All -'
            offset: false
            offset_label: Offset
          views_infinite_scroll:
            button_text: 'Load More'
            automatically_load_content: false
      block_description: 'Pests of season'
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - user
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.body'
        - 'config:field.storage.node.field_pest'
        - 'config:field.storage.node.field_season'
        - 'config:field.storage.node.field_this_season'
  page_1:
    display_plugin: page
    id: page_1
    display_title: Page
    position: 1
    display_options:
      display_extenders: {  }
      path: pests
    cache_metadata:
      max-age: -1
      contexts:
        - 'languages:language_content'
        - 'languages:language_interface'
        - url.query_args
        - user
        - 'user.node_grants:view'
        - user.permissions
      tags:
        - 'config:field.storage.node.body'
        - 'config:field.storage.node.field_pest'
        - 'config:field.storage.node.field_season'
        - 'config:field.storage.node.field_this_season'
 

wombatbuddy’s picture

I see that in your code you wrote:
$value = 'autumn';

What is correct term's name 'autumn' or 'fall'?

I used the 'fall' name, but if it is not correct, you can correct it itself.

tjtj’s picture

I arbitrarily used autumn

wombatbuddy’s picture

I have created the solution here: https://www.drupal.org/sandbox/wombatbuddy/3108024

Create the 'Has taxonomy term ID' contextual filter and install the module.

The module calculate the current season using the rules provided by client.
Then extracted term id of a term that has name which is equal to the current season.
After that the term id is passed into the view as the argument for the contextual filter.

tjtj’s picture

I downloaded the two files, put them in a module directory called views_season_argument, enabled it, and created the contextual filter as illustrated (using the defaults). But I do not see where to use your new module in the view. What is the ID to look for in the filter?

It is displaying the correct things for winter with no extra contextual filters,

wombatbuddy’s picture

You don't have to do anything.

tjtj’s picture

And thank you again. I learned a lot from your code. I have never understood before where to put the hook code.

wombatbuddy’s picture

If it’s not a secret, what pests are you talking about?

tjtj’s picture

http://jarfx.dyndns.org/bpc/bpc/web/contact

But I am working on getting https going.