metatag_devel.module in Metatag 7
Primary hook implementations for Metatag:Devel.
File
metatag_devel/metatag_devel.moduleView source
<?php
/**
* @file
* Primary hook implementations for Metatag:Devel.
*/
/**
* Implements hook_form_FORM_ID_alter() for devel_generate_content_form.
*
* Add extra options.
*/
function metatag_devel_form_devel_generate_content_form_alter(&$form, &$form_state, $form_id) {
$form['metatag_skip'] = array(
'#type' => 'select',
'#title' => t('Skip some meta tag values'),
'#options' => array(
0 => t("All meta tags given a value"),
2 => t('Every 2nd value'),
3 => t('Every 3rd value'),
4 => t('Every 4th value'),
5 => t('Every 5th value'),
),
'#default' => 0,
'#description' => t('Control whether all of the meta tags will be given values, or .'),
);
// Move the submit button to after the rest of the fields.
$form['submit']['#weight'] = 100;
}
/**
* Implements hook_node_insert().
*
* Integrate with Devel Generate.
*/
function metatag_devel_node_insert($node) {
// Check to see if the node is generated by Devel Generate.
if (isset($node->devel_generate)) {
$metatags = array();
// Max length of words.
$title_length = $node->devel_generate['title_length'];
// Pregenerate the URL.
$url = url('<front>', array(
'absolute' => TRUE,
));
// Types of images.
$image_types = array(
'jpg' => 'jpg',
'png' => 'png',
'gif' => 'gif',
);
// Optionally skip some meta tags.
$skip = $node->devel_generate['metatag_skip'];
// Generate some meta tags.
$count = 0;
foreach (metatag_get_info('tags') as $tag => $tag_info) {
// Optionally skip records.
if (empty($skip) || $count == 0 || $count % $skip == 0) {
// Default to ten words per tag.
$maxlength = 10;
// By default, just assume a simple text value.
$type = 'text';
if (strpos($tag, 'title') !== FALSE) {
$maxlength = $title_length;
}
// Allow tags to customize the generation settings.
if (!empty($tag_info['devel_generate'])) {
if (!empty($tag_info['devel_generate']['type'])) {
$type = $tag_info['devel_generate']['type'];
}
if (!empty($tag_info['devel_generate']['maxlength'])) {
$maxlength = $tag_info['devel_generate']['maxlength'];
}
}
elseif (isset($tag_info['form']) && is_array($tag_info['form'])) {
// Textarea fields can be longer than other tags.
if (isset($tag_info['form']['#type']) && $tag_info['form']['#type'] == 'textarea') {
$maxlength = 20;
}
elseif (isset($tag_info['form']['#options'])) {
$type = 'select';
}
}
// Simple values.
if ($type == 'text') {
$metatags[$tag]['value'] = devel_create_greeking($maxlength, TRUE);
}
elseif ($type == 'select') {
// Nested arrays, aka opgroups - collapse it down to one level.
if (is_array($tag_info['form']['#options'])) {
$options = array();
foreach ($tag_info['form']['#options'] as $option => $subopts) {
if (is_array($subopts)) {
$options += array_keys($subopts);
}
else {
$options[] = $option;
}
}
}
else {
$options = $tag_info['form']['#options'];
}
$metatags[$tag]['value'] = array_rand(drupal_map_assoc($options));
// Support checkboxes, which require the default value be an array.
if ($tag_info['class'] == 'DrupalListMetaTag') {
$metatags[$tag]['value'] = array(
$metatags[$tag]['value'],
);
}
}
elseif ($type == 'url') {
$metatags[$tag]['value'] = $url . strtolower(str_replace(' ', '/', devel_create_greeking($maxlength, TRUE)));
}
elseif ($type == 'image') {
$filepath = strtolower(str_replace(' ', '/', devel_create_greeking($maxlength, TRUE)));
$ext = array_rand($image_types);
$metatags[$tag]['value'] = $url . $filepath . '.' . $ext;
}
elseif ($type == 'integer') {
$metatags[$tag]['value'] = rand(0, 999);
}
elseif ($type == 'float') {
$metatags[$tag]['value'] = rand(0, 999) . '.' . rand(0, 999);
}
elseif ($type == 'phone') {
$metatags[$tag]['value'] = rand(100, 999) . '-' . rand(100, 999) . '-' . rand(1000, 9999);
}
elseif ($type == 'email') {
$metatags[$tag]['value'] = strtolower(devel_create_greeking(1, TRUE) . '@' . devel_create_greeking(1, TRUE) . '.com');
}
elseif ($type == 'canonical') {
$metatags[$tag]['value'] = '[current-page:url:absolute]';
}
elseif ($type == 'twitter') {
$metatags[$tag]['value'] = '@' . devel_create_greeking(1, TRUE);
}
// Replace spaces in keyword fields with commas.
if (strpos($tag, 'keyword') !== FALSE) {
$metatags[$tag]['value'] = str_replace(' ', ',', $metatags[$tag]['value']);
}
}
// Bump the counter.
$count++;
}
// Adjust the values for the nested language structure.
$metatags = array(
$node->language => $metatags,
);
// Save the meta tags.
metatag_metatags_save('node', $node->nid, $node->vid, $metatags);
}
}
Functions
Name | Description |
---|---|
metatag_devel_form_devel_generate_content_form_alter | Implements hook_form_FORM_ID_alter() for devel_generate_content_form. |
metatag_devel_node_insert | Implements hook_node_insert(). |