Skip to content

Instantly share code, notes, and snippets.

@indytechcook
Created December 11, 2011 14:18
Show Gist options
  • Select an option

  • Save indytechcook/1460818 to your computer and use it in GitHub Desktop.

Select an option

Save indytechcook/1460818 to your computer and use it in GitHub Desktop.

Revisions

  1. Neil Hastings revised this gist Dec 13, 2011. 1 changed file with 267 additions and 0 deletions.
    267 changes: 267 additions & 0 deletions custom_bean.listing.inc
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    <?php
    /**
    * @file
    * latest_article_listing.inc
    */
    class <?php
    /**
    * @file
    * latest_article_listing.inc
    @@ -263,6 +268,268 @@ class latest_article_bean extends bean_plugin {

    unset($content['bean']);

    return $content;
    }
    } extends bean_plugin {
    public function values() {
    return array(
    'nodes_view_mode' => array(
    'options' => '',
    ),
    'title_link' => array(
    'value' => NULL,
    ),
    'filters' => array(
    'offices' => FALSE,
    'term' => FALSE,
    'topic' => FALSE,
    'audience' => FALSE,
    ),
    'total_articles' => array(
    'value' => 0,
    ),
    'group_by_date' => array(
    'value' => 0,
    ),
    'more_link' => array(
    'text' => '',
    'path' => '',
    ),
    );
    }

    public function form($bean, $form, &$form_state) {
    $form = array();

    drupal_set_message(t('Please note that there are two Group Content fields to select. The first one (within the "Filters" box) determines what content is selected by the Bean filters. The second one ("Group_Audience") determines where the Bean can be placed and who has permission to use it. Please fill out both values.'));

    $nodes_view_modes = array(
    'block_title_only' => t('Title Only'),
    'block_teaser' => t('Title and Teaser'),
    'block_title_teaser_morelink' => t('Title, Teaser, and More Link'),
    'block_thumbnail_title' => t('Thumbnail and Title'),
    );

    $form['nodes_view_mode'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('View Mode'),
    );
    $form['nodes_view_mode']['option'] = array(
    '#type' => 'select',
    '#title' => t('Per Article Option'),
    '#description' => t('How should each article display?'),
    '#options' => $nodes_view_modes,
    '#default_value' => (isset($bean->nodes_view_mode['option']) ? $bean->nodes_view_mode['option'] : NULL),
    '#required' => TRUE,
    );

    $group_options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('offices')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $group = og_get_group('taxonomy_term', $term->tid);
    if (isset($group->gid)) {
    $group_options[$group->gid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }
    }

    $options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('article_type')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }

    $topic_options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('topics')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $topic_options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }

    $audience_options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('audience')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $audience_options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }

    $form['title_link'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Title for Link'),
    );
    $form['title_link']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Link for Title'),
    '#description' => t('This might be a topic page associated with the title, for example.'),
    '#default_value' => $bean->title_link['value'],
    '#size' => 40,
    );

    $form['filters'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Filters'),
    );
    $form['filters']['offices'] = array(
    '#type' => 'select',
    '#title' => t('Group Content for filtering'),
    '#description' => t('This group content setting will be used to filter the search. Group Audience below controls placement.'),
    '#options' => $group_options,
    '#default_value' => $bean->filters['offices'],
    '#multiple' => TRUE,
    '#size' => 10,
    );
    $form['filters']['term'] = array(
    '#type' => 'select',
    '#title' => t('Article Type'),
    '#options' => $options,
    '#default_value' => $bean->filters['term'],
    '#multiple' => TRUE,
    '#size' => 5,
    );
    $form['filters']['topic'] = array(
    '#type' => 'select',
    '#title' => t('Topic'),
    '#options' => $topic_options,
    '#default_value' => isset($bean->filters['topic']) ? $bean->filters['topic'] : '',
    '#multiple' => TRUE,
    '#size' => 10,
    );
    $form['filters']['audience'] = array(
    '#type' => 'select',
    '#title' => t('Audience'),
    '#options' => $audience_options,
    '#default_value' => isset($bean->filters['audience']) ? $bean->filters['audience'] : '',
    '#multiple' => TRUE,
    '#size' => 10,
    );

    $form['total_articles'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Articles to Display'),
    );
    $form['total_articles']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of articles to display'),
    '#description' => t('What is the maximum number of articles to display?'),
    '#default_value' => $bean->total_articles['value'],
    '#required' => TRUE,
    '#size' => 5,
    );

    $form['group_by_date'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Group by Date'),
    );
    $form['group_by_date']['value'] = array(
    '#type' => 'select',
    '#title' => t('Group by Date'),
    '#description' => t('Should articles by grouped by date?'),
    '#options' => array(0 => t('No'), 1 => t('Yes')),
    '#default_value' => $bean->group_by_date['value'],
    );

    $form['more_link'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('More link'),
    );

    $form['more_link']['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Link text'),
    '#default_value' => $bean->more_link['text'],
    );

    $form['more_link']['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Link path'),
    '#default_value' => $bean->more_link['path'],
    );

    return $form;
    }

    public function view($bean, $content, $view_mode = 'full', $langcode = NULL) {
    $count = $bean->total_articles['value'];

    $query = new EnergyEntityFieldQuery();
    $query
    ->entityCondition('bundle', 'article')
    ->clearAudienceConditions()
    ->range(0, $count);

    if (!empty($bean->filters['term'])) {
    $query->setTermCondition('field_article_type', $bean->filters['term']);
    }

    if (!empty($bean->filters['topic'])) {
    $query->setTopicCondition($bean->filters['topic']);
    }

    if (!empty($bean->filters['audience'])) {
    $query->setTermCondition('field_audience_term', $bean->filters['audience']);
    }

    if (!empty($bean->filters['offices'])) {
    $query->SetOtherAudienceCondition($bean->filters['offices']);
    }

    // fallback for pre-existing beans
    else {
    $query->setOtherAudienceCondition();
    }

    $result = $query->execute();
    $content = array();

    if (empty($result)) {
    $content['nodes']['#markup'] = t('No results');
    return $content;
    }

    $previous_date = NULL;
    foreach ($result['node'] as $node) {
    $nodes_view_mode = $bean->nodes_view_mode['option'];
    $node = node_load($node->nid, $node->vid);
    //only show one topic link
    $terms = $node->field_topic_term[LANGUAGE_NONE];
    if (!empty($terms) && is_array($terms)) {
    foreach ($terms as $key => $tid) {
    if ($key == 0) {
    continue;
    }
    else {
    unset($node->field_topic_term[LANGUAGE_NONE][$key]);
    }
    }
    }
    $content['nodes'][$node->nid] = node_view($node, $nodes_view_mode);
    $date = format_date($node->created, 'article_date');
    if (($previous_date != $date) && (bool) $bean->group_by_date['value']) {
    $content['nodes'][$node->nid]['#prefix'] = theme('block_custom_group_by_date', array('date' => $date));
    }
    if ($nodes_view_mode == 'block_title_teaser_morelink' && !empty($bean->more_link['path']) && !empty($bean->more_link['text'])) {
    $content['nodes'][$node->nid]['#suffix'] = theme('block_custom_more_link', array('text' => $bean->more_link['text'], 'path' => 'node/' . $node->nid));
    }
    $previous_date = $date;
    }

    unset($content['bean']);

    return $content;
    }
    }
  2. Neil Hastings revised this gist Dec 13, 2011. 3 changed files with 260 additions and 39 deletions.
    253 changes: 222 additions & 31 deletions custom_bean.listing.inc
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,192 @@
    <?php

    /**
    * @file
    * Listing bean plugin.
    * latest_article_listing.inc
    */

    class listing_bean extends bean_plugin {

    /**
    * Declares default block settings.
    */
    class latest_article_bean extends bean_plugin {
    public function values() {
    return array(
    'nodes_view_mode' => array(
    'options' => '',
    ),
    'title_link' => array(
    'value' => NULL,
    ),
    'filters' => array(
    'offices' => FALSE,
    'term' => FALSE,
    'topic' => FALSE,
    'audience' => FALSE,
    ),
    'total_articles' => array(
    'value' => 0,
    ),
    'group_by_date' => array(
    'value' => 0,
    ),
    'more_link' => array(
    'text' => '',
    'path' => '',
    ),
    );
    }

    /**
    * Builds extra settings for the block edit form.
    */
    public function form($bean, $form, &$form_state) {
    $form = array();

    drupal_set_message(t('Please note that there are two Group Content fields to select. The first one (within the "Filters" box) determines what content is selected by the Bean filters. The second one ("Group_Audience") determines where the Bean can be placed and who has permission to use it. Please fill out both values.'));

    $nodes_view_modes = array(
    'block_title_only' => t('Title Only'),
    'block_teaser' => t('Title and Teaser'),
    'block_title_teaser_morelink' => t('Title, Teaser, and More Link'),
    'block_thumbnail_title' => t('Thumbnail and Title'),
    );

    $form['nodes_view_mode'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('View Mode'),
    );
    $form['nodes_view_mode']['option'] = array(
    '#type' => 'select',
    '#title' => t('Per Article Option'),
    '#description' => t('How should each article display?'),
    '#options' => $nodes_view_modes,
    '#default_value' => (isset($bean->nodes_view_mode['option']) ? $bean->nodes_view_mode['option'] : NULL),
    '#required' => TRUE,
    );

    $group_options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('offices')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $group = og_get_group('taxonomy_term', $term->tid);
    if (isset($group->gid)) {
    $group_options[$group->gid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }
    }

    $options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('article_type')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }

    $topic_options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('topics')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $topic_options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }

    $audience_options = array();
    if ($vocabulary = taxonomy_vocabulary_machine_name_load('audience')) {
    if ($terms = taxonomy_get_tree($vocabulary->vid)) {
    foreach ($terms as $term) {
    $audience_options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
    }
    }

    $form['title_link'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Title for Link'),
    );
    $form['title_link']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Link for Title'),
    '#description' => t('This might be a topic page associated with the title, for example.'),
    '#default_value' => $bean->title_link['value'],
    '#size' => 40,
    );

    $form['filters'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Filters'),
    );
    $form['filters']['offices'] = array(
    '#type' => 'select',
    '#title' => t('Group Content for filtering'),
    '#description' => t('This group content setting will be used to filter the search. Group Audience below controls placement.'),
    '#options' => $group_options,
    '#default_value' => $bean->filters['offices'],
    '#multiple' => TRUE,
    '#size' => 10,
    );
    $form['filters']['term'] = array(
    '#type' => 'select',
    '#title' => t('Article Type'),
    '#options' => $options,
    '#default_value' => $bean->filters['term'],
    '#multiple' => TRUE,
    '#size' => 5,
    );
    $form['filters']['topic'] = array(
    '#type' => 'select',
    '#title' => t('Topic'),
    '#options' => $topic_options,
    '#default_value' => isset($bean->filters['topic']) ? $bean->filters['topic'] : '',
    '#multiple' => TRUE,
    '#size' => 10,
    );
    $form['filters']['audience'] = array(
    '#type' => 'select',
    '#title' => t('Audience'),
    '#options' => $audience_options,
    '#default_value' => isset($bean->filters['audience']) ? $bean->filters['audience'] : '',
    '#multiple' => TRUE,
    '#size' => 10,
    );

    $form['total_articles'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Articles to Display'),
    );
    $form['total_articles']['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Number of articles to display'),
    '#description' => t('What is the maximum number of articles to display?'),
    '#default_value' => $bean->total_articles['value'],
    '#required' => TRUE,
    '#size' => 5,
    );

    $form['group_by_date'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('Group by Date'),
    );
    $form['group_by_date']['value'] = array(
    '#type' => 'select',
    '#title' => t('Group by Date'),
    '#description' => t('Should articles by grouped by date?'),
    '#options' => array(0 => t('No'), 1 => t('Yes')),
    '#default_value' => $bean->group_by_date['value'],
    );

    $form['more_link'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('More link'),
    );

    $form['more_link']['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Link text'),
    '#default_value' => $bean->more_link['text'],
    );

    $form['more_link']['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Link path'),
    @@ -42,36 +195,74 @@ class listing_bean extends bean_plugin {

    return $form;
    }

    /**
    * Displays the bean.
    */
    public function view($bean, $content, $view_mode = 'list', $langcode = NULL) {

    $query = new EntityFieldQuery();
    public function view($bean, $content, $view_mode = 'full', $langcode = NULL) {
    $count = $bean->total_articles['value'];

    $query = new EnergyEntityFieldQuery();
    $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'article')
    ->propertyCondition('status', 1)
    ->propertyOrderBy('created', 'DESC')
    ->range(0, 5);
    ->clearAudienceConditions()
    ->range(0, $count);

    if (!empty($bean->filters['term'])) {
    $query->setTermCondition('field_article_type', $bean->filters['term']);
    }

    if (!empty($bean->filters['topic'])) {
    $query->setTopicCondition($bean->filters['topic']);
    }

    if (!empty($bean->filters['audience'])) {
    $query->setTermCondition('field_audience_term', $bean->filters['audience']);
    }

    if (!empty($bean->filters['offices'])) {
    $query->SetOtherAudienceCondition($bean->filters['offices']);
    }

    // fallback for pre-existing beans
    else {
    $query->setOtherAudienceCondition();
    }

    $result = $query->execute();
    $content = array();

    if (empty($result)) {
    $content['nodes'] = array();
    $content['nodes']['#markup'] = t('No results');
    return $content;
    }
    else {
    $content['nodes'] = node_load_multiple(array_keys($result['node']));

    $previous_date = NULL;
    foreach ($result['node'] as $node) {
    $nodes_view_mode = $bean->nodes_view_mode['option'];
    $node = node_load($node->nid, $node->vid);
    //only show one topic link
    $terms = $node->field_topic_term[LANGUAGE_NONE];
    if (!empty($terms) && is_array($terms)) {
    foreach ($terms as $key => $tid) {
    if ($key == 0) {
    continue;
    }
    else {
    unset($node->field_topic_term[LANGUAGE_NONE][$key]);
    }
    }
    }
    $content['nodes'][$node->nid] = node_view($node, $nodes_view_mode);
    $date = format_date($node->created, 'article_date');
    if (($previous_date != $date) && (bool) $bean->group_by_date['value']) {
    $content['nodes'][$node->nid]['#prefix'] = theme('energy_group_by_date', array('date' => $date));
    }
    if ($nodes_view_mode == 'block_title_teaser_morelink' && !empty($bean->more_link['path']) && !empty($bean->more_link['text'])) {
    $content['nodes'][$node->nid]['#suffix'] = theme('energy_more_link', array('text' => $bean->more_link['text'], 'path' => 'node/' . $node->nid));
    }
    $previous_date = $date;
    }

    $content['#theme'] = 'bean_list'; // You need to do something here yourself.
    $content['more_link'] = array(
    'text' => $bean->more_link['text'],
    'path' => $bean->more_link['path'],
    );

    unset($content['bean']);

    return $content;
    }

    }
    37 changes: 29 additions & 8 deletions custom_bean.module
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,45 @@
    <?php
    /**
    * Implements hook_bean_types_api_info().
    */
    function custom_beans_bean_types_api_info() {
    return array( 'api' => 3);
    /**
    * Implements hook_bean_types_api_info().
    */
    function custom_beans_bean_types_api_info() {
    return array( 'api' => 3);
    }

    /**
    * Implements hook_bean_types().
    */
    function custom_beans_bean_types() {
    $plugins = array();

    $plugins['listing'] = array(
    $plugins['latest_article_bean'] = array(
    'label' => t('Listing'),
    'handler' => array(
    'class' => 'listing_bean',
    'class' => 'blog_listing_bean',
    'parent' => 'bean',
    ),
    );

    return $plugins;
    }

    function custom_beans_theme() {
    $items = array();

    $items['custom_bean_group_by_date'] = array(
    'arguments' => array(
    'date' => NULL,
    ),
    'file' => 'custom_bean.theme.inc',
    );

    $items['custom_bean_more_link'] = array(
    'arguments' => array(
    'text' => NULL,
    'path' => NULL,
    ),
    'file' => 'custom_bean.theme.inc',
    );

    return $items;
    }
    9 changes: 9 additions & 0 deletions custom_bean.theme.inc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?php

    function theme_custom_bean_group_by_date(&$variables) {
    return '<div class="group-by-date">' . $variables['date'] . '</div>';
    }

    function theme_custom_bean_more_link(&$variables) {
    return l($variables['text'], $variables['path'], array('attributes' => array('class' => array('more-link'))));
    }
  3. Neil Hastings created this gist Dec 11, 2011.
    4 changes: 4 additions & 0 deletions custom_bean.info
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    name = Custom Beans
    description = My Custom Beans
    core = 7.x
    files[] = "custom_bean.listing.inc"
    77 changes: 77 additions & 0 deletions custom_bean.listing.inc
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    <?php

    /**
    * @file
    * Listing bean plugin.
    */

    class listing_bean extends bean_plugin {

    /**
    * Declares default block settings.
    */
    public function values() {
    return array(
    'more_link' => array(
    'text' => '',
    'path' => '',
    ),
    );
    }

    /**
    * Builds extra settings for the block edit form.
    */
    public function form($bean, $form, &$form_state) {
    $form = array();
    $form['more_link'] = array(
    '#type' => 'fieldset',
    '#tree' => 1,
    '#title' => t('More link'),
    );
    $form['more_link']['text'] = array(
    '#type' => 'textfield',
    '#title' => t('Link text'),
    '#default_value' => $bean->more_link['text'],
    );
    $form['more_link']['path'] = array(
    '#type' => 'textfield',
    '#title' => t('Link path'),
    '#default_value' => $bean->more_link['path'],
    );

    return $form;
    }

    /**
    * Displays the bean.
    */
    public function view($bean, $content, $view_mode = 'list', $langcode = NULL) {

    $query = new EntityFieldQuery();
    $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'article')
    ->propertyCondition('status', 1)
    ->propertyOrderBy('created', 'DESC')
    ->range(0, 5);

    $result = $query->execute();

    if (empty($result)) {
    $content['nodes'] = array();
    }
    else {
    $content['nodes'] = node_load_multiple(array_keys($result['node']));
    }

    $content['#theme'] = 'bean_list'; // You need to do something here yourself.
    $content['more_link'] = array(
    'text' => $bean->more_link['text'],
    'path' => $bean->more_link['path'],
    );

    return $content;
    }

    }
    24 changes: 24 additions & 0 deletions custom_bean.module
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?php
    /**
    * Implements hook_bean_types_api_info().
    */
    function custom_beans_bean_types_api_info() {
    return array( 'api' => 3);
    }

    /**
    * Implements hook_bean_types().
    */
    function custom_beans_bean_types() {
    $plugins = array();

    $plugins['listing'] = array(
    'label' => t('Listing'),
    'handler' => array(
    'class' => 'listing_bean',
    'parent' => 'bean',
    ),
    );

    return $plugins;
    }