Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bean Listing Exammple
name = Custom Beans
description = My Custom Beans
core = 7.x
files[] = "custom_bean.listing.inc"
<?php
/**
* @file
* latest_article_listing.inc
*/
class <?php
/**
* @file
* latest_article_listing.inc
*/
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' => '',
),
);
}
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('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;
}
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;
}
}
<?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['latest_article_bean'] = array(
'label' => t('Listing'),
'handler' => array(
'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;
}
<?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'))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.