function faqfield_field_formatter_view in FAQ Field 7
Implements hook_field_formatter_view().
File
- ./
faqfield.module, line 370 - FAQ Field Provides a field for frequently asked questions.
Code
function faqfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
// If there are no items to process, we've nothing to do here.
if (empty($items)) {
return $element;
}
switch ($display['type']) {
// This formatter adds jQuery accordion widget.
// This will not be themeable, because changes would break
// jQuery UI accordion functionality!
case 'faqfield_accordion':
// Generate faqfield id by fieldname and entity id.
$entity_ids = entity_extract_ids($entity_type, $entity);
$faqfield_id = 'faqfield_' . $field['field_name'] . '_' . $entity_ids[0];
// We need to convert the element active value to an integer for jQuery.
if ($display['settings']['active'] !== '') {
settype($display['settings']['active'], 'int');
}
else {
$display['settings']['active'] = FALSE;
}
// If jquery_update module is installed and a version >1.5 is used
// the "autoHeight" option has been replaced with "heightStyle".
// To be compatible to higher jQuery versions we have to switch accordingly.
if (module_exists('jquery_update') && variable_get('jquery_update_jquery_version') !== '1.5') {
if ($display['settings']['autoHeight']) {
$display['settings']['heightStyle'] = 'auto';
}
else {
$display['settings']['heightStyle'] = 'content';
}
unset($display['settings']['autoHeight']);
}
// Attach accordion JS library and related display settings.
$element[0]['#attached']['library'][] = array(
'faqfield',
'accordion',
);
$element[0]['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'faqfield' => array(
'#' . $faqfield_id => $display['settings'],
),
),
);
// We need to put all of this within a single piece of markup,
// otherwise this would not work with jQuery accordion.
$element[0]['#markup'] = '<div id="' . $faqfield_id . '">';
foreach ($items as $key => $item) {
// Decide whether to use the default format or the custom one.
$format = !empty($item['answer_format']) ? $item['answer_format'] : $field['settings']['format'];
// Build the markup.
$name = 'faq-' . str_replace(' ', '-', $item['question']);
// Store the raw anchor name so the accordion item can be unfurled.
$anchors[$name] = $key;
$name = check_plain($name);
$element[0]['#markup'] .= '<h3 class="faqfield-question" id="' . $name . '"><a href="#' . $name . '">' . check_plain($item['question']) . '</a></h3>';
$element[0]['#markup'] .= '<div class="faqfield-answer">' . check_markup($item['answer'], $format) . '</div>';
}
$element[0]['#markup'] .= '</div>';
$element[0]['#attached']['js'][] = array(
'type' => 'setting',
'data' => array(
'faqfieldAnchors' => $anchors,
),
);
break;
// This themeable formatter displays the FAQ content as simple text.
case 'faqfield_simple_text':
foreach ($items as $delta => $item) {
// Decide whether to use the default format or the custom one.
$format = !empty($item['answer_format']) ? $item['answer_format'] : $field['settings']['format'];
// Add them as page elements, they'll be rendered automatically later.
$element[$delta] = array(
'#theme' => 'faqfield_formatter',
// Filter values before passing them to the template.
'#question' => check_plain($item['question']),
'#answer' => check_markup($item['answer'], $format),
'#delta' => $delta,
);
}
break;
/*
* This formatter displays the FAQ content as definition list, and has been
* updated with the addition of accordion functionality.
*
* Taken from the Web Accessibility for Developers course offered by
* The Chang School at Ryerson University, for details see:
* https://www.canvas.net/browse/ryersonu/courses/adv-web-accessibility
*/
case 'faqfield_definition_list':
drupal_add_js(drupal_get_path('module', 'faqfield') . '/js/faqfield.accordion_dl.js', array(
'scope' => 'header',
'weight' => 51,
));
drupal_add_css(drupal_get_path('module', 'faqfield') . '/css/faqfield.accordion_dl.css');
// Need to add "multiselect" class to work with above javascript.
$element[0]['#markup'] = '<dl class="faqfield-definition-list multiselect">';
foreach ($items as $item) {
// Decide whether to use the default format or the custom one.
$format = !empty($item['answer_format']) ? $item['answer_format'] : $field['settings']['format'];
// Build the markup; adding aria-hidden span with target inside to allow
// for designers to target with text or graphic open/close indicators.
$element[0]['#markup'] .= '<dt class="faqfield-question"><span class="indicator" aria-hidden="true"><span class="indicator-target"></span></span>' . check_plain($item['question']) . '</dt>';
$element[0]['#markup'] .= '<dd class="faqfield-answer">' . check_markup($item['answer'], $format) . '</dd>';
}
$element[0]['#markup'] .= '</dl>';
break;
// This formatter displays the FAQ content as anchors and text.
case 'faqfield_anchor_list':
$element[0]['#markup'] = '<div class="faqfield-anchor-list"><' . $display['settings']['anchor-list-type'] . '>';
$answers_markup = '';
foreach ($items as $item) {
// Build the anchor link list markup.
$name = 'faq-' . check_plain(str_replace(' ', '-', $item['question']));
$element[0]['#markup'] .= '<li><a href="#' . $name . '">' . check_plain($item['question']) . '</a></li>';
// Build the answer text markup.
$answers_markup .= '<h3 class="faqfield-question" id="' . $name . '"><a href="#' . $name . '">' . check_plain($item['question']) . '</a></h3>';
// Decide whether to use the default format or the custom one.
$format = !empty($item['answer_format']) ? $item['answer_format'] : $field['settings']['format'];
$answers_markup .= '<div class="faqfield-answer">' . check_markup($item['answer'], $format) . '</div>';
}
$element[0]['#markup'] .= '</' . $display['settings']['anchor-list-type'] . '>';
// Now attach the answers text markup.
$element[0]['#markup'] .= $answers_markup . '</div>';
break;
}
return $element;
}