Commit e08760f9 authored by DamienMcKenna's avatar DamienMcKenna 🤦 Committed by DamienMcKenna
Browse files

Issue #3061475 by DamienMcKenna: Token to provide summary of text fields

parent 274108f7
......@@ -13,6 +13,7 @@ Metatag 7.x-1.x, xxxx-xx-xx
#3037874 by Ronino: Build meta tags for Panels much later so they can be altered
after page data has been processed.
#3018692 by thatguy, DamienMcKenna: Search API index multiple types.
#3061475 by DamienMcKenna: Token to provide summary of text fields.
Metatag 7.x-1.25, 2018-04-03
......
......@@ -674,6 +674,19 @@ function metatag_admin_settings_form() {
'#default_value' => variable_get('metatag_pager_string', 'Page PAGER | '),
);
$form['additional']['metatag_summary_text_field'] = array(
'#title' => t('Add summary tokens for regular text fields'),
'#type' => 'checkbox',
'#description' => t("By default Metatag's summary tokens are added to long text fields. Enabling this option will also add summary tokens for all plain text fields."),
'#default_value' => variable_get('metatag_summary_text_field', FALSE),
);
$form['additional']['metatag_summary_length'] = array(
'#title' => t('Length of summary'),
'#type' => 'textfield',
'#description' => t("How long each of the summary tokens will be after it is truncated."),
'#default_value' => variable_get('metatag_summary_length', 200),
);
$form['i18n'] = array(
'#type' => 'fieldset',
'#title' => t('Internationalization options'),
......@@ -827,6 +840,19 @@ function metatag_admin_settings_form_submit($form, &$form_state) {
}
}
// If the token option was disabled, clear the token cache too.
$old_var = (bool) variable_get('metatag_summary_text_field', FALSE);
$new_var = !empty($form_state['values']['metatag_summary_text_field']);
if ($old_var != $new_var) {
token_clear_cache();
}
// Make sure the metatag_summary_length value is a number.
if (!empty($form_state['values']['metatag_summary_length'])) {
$form_state['values']['metatag_summary_length'] = intval($form_state['values']['metatag_summary_length']);
}
// Clear the Metatag caches so any settings changes will be available.
cache_clear_all('*', 'cache_metatag', TRUE);
drupal_set_message(t('The Metatag cache has been cleared so all meta tags can be reloaded.'));
}
......@@ -503,6 +503,12 @@ function metatag_uninstall() {
variable_del('metatag_maxlength_abstract');
variable_del('metatag_maxlength_keywords');
variable_del('metatag_maxlength_news_keywords');
// Whether regular text fields can have summary tokens too.
variable_del('metatag_summary_text_field');
// Length of summaries generated by the custom tokens.
variable_del('metatag_summary_length');
}
/**
......
......@@ -63,6 +63,28 @@ function metatag_token_info() {
'description' => t('A custom pager (from the Metatag module). Currently set to "@pager" which would be output as e.g. "@page".', array('@pager' => $pager, '@page' => $page)),
);
// Custom summary tokens for each long text field.
$supported_types = array('text_long', 'text_with_summary');
if (variable_get('metatag_summary_text_field', FALSE)) {
$supported_types[] = 'text';
}
$trim_length = variable_get('metatag_summary_length', 200);
foreach (field_read_fields() as $field_name => $field) {
if (!empty($field['module'])) {
if (in_array($field['type'], $supported_types)) {
$instances = field_read_instances(array('field_name' => $field_name));
foreach ($instances as $instance) {
if (!empty($instance['entity_type']) && !empty($instance['label'])) {
$info['tokens'][$instance['entity_type']][$field_name . '-summary'] = array(
'name' => t('@label (summary)', array('@label' => $instance['label'])),
'description' => t('A summary of the @label field, trimmed to @length characters.', array('@label' => $instance['label'], '@length' => $trim_length)),
);
}
}
}
}
}
return $info;
}
......@@ -128,6 +150,50 @@ function metatag_tokens($type, $tokens, array $data = array(), array $options =
}
}
// Summary tokens. It is safe to rely upon an "entity" token type here because
// it is added by the Token module.
if ($type == 'entity' && isset($options['entity'], $options['entity_type'])) {
$trim_length = variable_get('metatag_summary_length', 200);
foreach ($tokens as $name => $original) {
if (substr($name, -8) == '-summary') {
// The field name is the token name minus "-summary".
$field_name = substr($name, 0, -8);
// Work out the requested language, if appropriate.
if (isset($options['language'])) {
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$entity = $options['entity'];
$entity_type = $options['entity_type'];
list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);
$items = field_get_items($entity_type, $entity, $field_name, $language_code);
if (!empty($items)) {
$instance = field_info_instance($entity_type, $field_name, $bundle);
$field_langcode = field_language($entity_type, $entity, $field_name, $language_code);
// If the summary was filled in it is not empty, use it.
if (!empty($items[0]['summary'])) {
$output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'summary') : $items[0]['summary'];
}
// Attempt to provide a suitable version of the 'body' field.
else {
$output = $sanitize ? _text_sanitize($instance, $field_langcode, $items[0], 'value') : $items[0]['value'];
}
if (!empty($output)) {
// Trim the summary.
$output = text_summary($output, $instance['settings']['text_processing'] ? $items[0]['format'] : NULL, $trim_length);
if (!empty($output)) {
$replacements[$original] = $output;
}
}
}
}
}
}
return $replacements;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment