1

I have created a calendar module in Drupal 7. In addition to the default calendar on the website, my client can create a new calendar for events specific to a particular page.

Say that they want to add "Youth" and "College and Career" calendars; the db table looks like this:

table: churchcal_calendars

------id------|------name-----|------set_default------

------1-------------Main---------------1--------

------2-------------Youth--------------0---------

------3-------College and Career---------0-----------

In my churchcal.module file, I have set hook_block_info and hook_block_view:

function churchcal_block_info() {
    $blocks = array();
    $blocks['monthly_calendar'] = array(
        'info' => t('Display the monthly calendar.'),
        'cache' => DRUPAL_NO_CACHE,
    );

    //get calendars
    include('db.php');
    $result = mysql_query("SELECT * FROM churchcal_calendars WHERE set_default != '1'");
        while($row = mysql_fetch_array($result)) {      
            $cal_name = $row['name'];
            $sys_cal_name = strtolower(str_replace(' ', '_', $cal_name));
            $blocks['churchcal_' . $sys_cal_name] = array(
                'info' => t('Display calendar for ' . $cal_name . '.'),
                'cache' => DRUPAL_NO_CACHE,
            );
        }

    return $blocks;
}

function churchcal_block_view($block_name = '') {
    if ($block_name == 'monthly_calendar') {
        $content = monthlyCalendar();
        $block = array (
            'subject' => t(''),
            'content' => $content,
        );
    }

        //get calendars
        include('db.php');
        $result = mysql_query("SELECT * FROM churchcal_calendars WHERE set_default != '1'");
            while($row = mysql_fetch_array($result)) {
                $cal_name = $row['name'];
                $sys_cal_name = strtolower(str_replace(' ', '_', $cal_name));
                $cal_id = $row['id'];

                if($block_name == 'churchcal_' . $sys_cal_name) {
                    $content .= churchCal($cal_id);
                    $block = array(
                        'subject' => t('Upcoming Events'),
                        'content' => $content,
                    );
            }

        return $block;
    }
}

The "monthly_calendar" is the main calendar, and there is no problem with that block.

Beneath the monthly calendar is the code for the additional calendars. I can see each calendar in admin/structure/block page (for the example "Display calendar for Youth", "Display calendar for College and Career"). That part works great. However, I can only see one calendar, and that is whichever is first in the db with default != 1. In this example, the Youth calendar would display, but not the College and Career. So, it tells me that hook_block_info is correct and there is a problem with hook_block_view.

How can I display a calendar for each calendar in the db?

3
  • I figured it out ... dumb mistake. I inserted "return $block;" in the while() statement and didn't realize. Commented Aug 2, 2012 at 19:09
  • Post your solution below and accept it as the answer, so that this question is marked as solved...
    – Sharan
    Commented Aug 2, 2012 at 20:26
  • I can't because I haven't posted my quota here. Commented Aug 2, 2012 at 21:24

1 Answer 1

1

It looks like you should review that "return $block;" that you inserted in the while() statement.

PS: not sure why you didn't (couldn't) post your own answer, which is why I do so now anyway ...

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.