Hi all

With the below code I select some entities no older than today.

$query = new EntityFieldQuery();
  $query
      ->entityCondition('entity_type', 'whats_on')
      ->entityCondition('bundle', 'events')
      ->fieldCondition('field_event_date_time', 'value', date("Y-m-d"), '>') // Show events no earlier than today
      ->fieldOrderBy('field_event_date_time', 'value', 'ASC')

Is it possible to show events only for this week, meaning, I'll need to somehow track what day it is and display only the events for the days remaining till Sunday.

Example:
If today is Friday, we only show events for Friday, Saturday and Sunday (only 3 days left in this week).

Is this something we can do with fieldCondition, maybe a small snippet of PHP we can add above as shown below, which I did try but only shows events after 7 days?

->fieldCondition('field_event_date_time', 'value', date("Y-m-d",strtotime("+1 week")), '>')

Thanks, Barry

Comments

nevets’s picture

Have you tried

$query = new EntityFieldQuery();
  $query
      ->entityCondition('entity_type', 'whats_on')
      ->entityCondition('bundle', 'events')
      ->fieldCondition('field_event_date_time', 'value', date("Y-m-d"), '>') // Show events no earlier than today
      ->fieldCondition('field_event_date_time', 'value', date("Y-m-d",strtotime("+1 week")), '<')
      ->fieldOrderBy('field_event_date_time', 'value', 'ASC')
computerbarry’s picture

Good call.
This is working, now I have events showing for the next 7 days looks good :)

Now within these events I'm getting results from Tuesday, Wednesday next week.
How do I only show events for this week? Maximum return should be Sunday/Monday morning.

I could easily add strtotime("+3 days"), though come tomorrow this would be invalid.

Thanks, Barry

The more you learn.... the more you learn there is more to learn.

nevets’s picture

$dow = date('N');
if ( $dow == 1 ) { // Monday
  $offset = 'today';
}
else {
  $offset = 'next monday';
}
$query = new EntityFieldQuery();
  $query
      ->entityCondition('entity_type', 'whats_on')
      ->entityCondition('bundle', 'events')
      ->fieldCondition('field_event_date_time', 'value', date("Y-m-d"), '>') // Show events no earlier than today
      ->fieldCondition('field_event_date_time', 'value', date("Y-m-d",strtotime($offset)), '<')
      ->fieldOrderBy('field_event_date_time', 'value', 'ASC')
computerbarry’s picture

Perfect!
Works great Nevets, just what I wanted :)

Thank you!

The more you learn.... the more you learn there is more to learn.

computerbarry’s picture

Out of curiosity, and no doubt an issue maybe further down the line.

What happens if not events are available for this week, would that mean no events are shown, or events for next will show as default?

Thanks, Barry

The more you learn.... the more you learn there is more to learn.

nevets’s picture

No events would be shown.

computerbarry’s picture

Ok.
Would it possible to code this in so we show the next available week?
Or maybe just add a else if and show a simple message "No events this week"

Thanks, Barry

The more you learn.... the more you learn there is more to learn.

nevets’s picture

As part of the code not shown, you execute the query, then loop through the results to list the events. You can enhance that code so if there are no results you print a message.

computerbarry’s picture

I've changed the dates of the events and you're right, nothing shows.
I tried adding if else myself, no idea where I should place this.

Can you advise nevets, just can't figure out where I should code this in.

Is it something to do with items at the bottom?

'#items' => $items,

We just need to drop in something like:

else {
    print "No events this week";
  }
function fulleventlist_block_eventlist($setrange, $setdate) {
  
  $dow = date('N');
  
  if ( $dow == 1 ) { // Monday
    $offset = 'today';
  }
  else {
    $offset = 'next monday';
  }
  
  $query = new NodeFieldQuery('event', 'field_event_date_and_time');
  $query->fieldCondition('field_event_date_and_time', 'value', date("Y-m-d"), '>'); // end date after today
  $query->fieldCondition('field_event_date_and_time', 'value', date("Y-m-d",strtotime($offset)), '<');
  $query->range(0, $setrange);
  $result = $query->execute();

  $nodes = array();

  if (isset($result['node'])) {
    $ids = array_keys($result['node']);
    $nodes = entity_load('node', $ids);
  }
  $sorted = array();
  $node_count = count($nodes);

  foreach ($nodes as $node) {

    // First put the date in a variable
    $date = dateString(new DateTime(date("$setdate", strtotime($node->field_event_date_and_time['und'][0]['value']))));
    $starts = date("g:ia", strtotime($node->field_event_date_and_time['und'][0]['value']));

    // next generate the data but use a few rows to make it easier to see
    $data = l(short_name($node->title, 70), 'node/' . $node->nid, array('attributes' => array('title' => $node->title, 'aria' => 'link')));
    $data .= '<div>';
    $data .= t('Starts: !starts', array('!starts' => $starts));
    $data .= '</div>';
    $sorted[$date][] = $data;
  }

  $items = array();
  foreach ($sorted as $date => $links) {
    $items[] = array
      (
      'data' => '<h3>' . $date . '</h3>',
      'children' => $links,
      'class' => array('list-group-item'),
    );
  }
  return array
    (
    'date_list' => array
      (
      '#prefix' => '<div class="header">Whats on this week</div>',
      '#theme' => 'item_list',
      '#items' => $items,
      '#attributes' => array('class' => array('list-group')),
      '#suffix' => '</div>',
    ),
  );
}

Thanks, Barry

The more you learn.... the more you learn there is more to learn.

computerbarry’s picture

I've managed to display the string using:

if (isset($result['node'])) {
    $ids = array_keys($result['node']);
    $nodes = entity_load('node', $ids);
  } else {
    echo 'No events this week';
  }

Though this is now showing outside of all my html straight after the opening body tag, is this the correct way?

How do I add this message inside the below output so everything sits inside the container with the title?

return array
    (
    'date_list' => array
      (
      '#prefix' => '<div class="header">Whats on this week</div>',
      '#theme' => 'item_list',
      '#items' => $items,
      '#attributes' => array('class' => array('list-group')),
      '#suffix' => '</div>',
    ),
  );

Thanks, barry

The more you learn.... the more you learn there is more to learn.

computerbarry’s picture

Can anybody help with this?

I just read over on theme_item_list api comments:

If the function that retrieves the uids returns an empty array, the resulting html will not have any li element.

This is exactly what's happening when no events are returned for this week
My markup:

<ul>
blank
</ul>

So how can we feed in the no events message into a new < li > when the array is empty?

Thanks, barry

The more you learn.... the more you learn there is more to learn.

Jaypan’s picture

  $items = array();
  foreach ($sorted as $date => $links) {
    $items[] = array
      (
      'data' => '<h3>' . $date . '</h3>',
      'children' => $links,
      'class' => array('list-group-item'),
    );
  }
  if(!count($items))
  {
    $items[] = t('No events found');
  }
computerbarry’s picture

Fantastic! Thanks a lot Jaypan.
Working perfectly. I should know this this by now all the work we've done :)

I also added another class into the mix:

if(!count($items))
    {
      $items[] = array
          (
        'data' => 'No events found.',
        'class' => array('none-listed'),
        );
    }

Thank you!

Barry

The more you learn.... the more you learn there is more to learn.

Jaypan’s picture

You should wrap 'no events found' in the t() function.

computerbarry’s picture

Good catch thanks.
Updated!

Barry

The more you learn.... the more you learn there is more to learn.