first import
This commit is contained in:
102
sites/all/modules/custom_formatters/engines/php.export.tpl.php
Normal file
102
sites/all/modules/custom_formatters/engines/php.export.tpl.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
*/
|
||||
?>
|
||||
/**
|
||||
* Implements hook_field_formatter_info().
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_info() {
|
||||
return array(
|
||||
'<?php echo $module; ?>_<?php echo $item->name; ?>' => array(
|
||||
'label' => t('<?php echo $item->label; ?>'),
|
||||
'field types' => array('<?php echo implode('\', \'', drupal_explode_tags($item->field_types)); ?>'),
|
||||
<?php if (isset($item->form)) : ?>
|
||||
'settings' => array(
|
||||
<?php foreach ($item->form as $form_key => $element) : ?>
|
||||
'<?php echo $form_key; ?>' => <?php if (is_array($element['#default_value'])) : ?>
|
||||
array(
|
||||
<?php foreach ($element['#default_value'] as $key => $value) : ?>
|
||||
<?php echo is_integer($key) ? $key : "'{$key}'"; ?> => '<?php echo $value; ?>',
|
||||
<?php endforeach; ?>
|
||||
),
|
||||
<?php else: ?>
|
||||
'<?php echo $element['#default_value']; ?>',
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
),
|
||||
<?php endif; ?>
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
<?php if (isset($item->form)) : ?>
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_summary().
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_settings_summary($field, $instance, $view_mode) {
|
||||
$display = $instance['display'][$view_mode];
|
||||
$settings = $display['settings'];
|
||||
|
||||
$summary = '';
|
||||
|
||||
if ($display['type'] == '<?php echo $module; ?>_<?php echo $item->name; ?>') {
|
||||
<?php foreach (element_children($item->form) as $key) : ?>
|
||||
// <?php echo $item->form[$key]['#title'] ?>.
|
||||
$value = empty($settings['<?php echo $key; ?>']) ? '<em>' . t('Empty') . '</em>' : $settings['<?php echo $key; ?>'];
|
||||
$value = is_array($value) ? implode(', ', array_filter($value)) : $value;
|
||||
$summary .= "<?php echo $item->form[$key]['#title'] ?>: {$value}<br />";
|
||||
<?php endforeach; ?>
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_settings_form().
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
|
||||
$display = $instance['display'][$view_mode];
|
||||
$settings = $display['settings'];
|
||||
|
||||
<?php foreach (explode("\n", $item->fapi) as $line) : ?>
|
||||
<?php echo $line . "\n"; ?>
|
||||
<?php endforeach; ?>
|
||||
return $form;
|
||||
}
|
||||
<?php endif; ?>
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_view().
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
|
||||
$element = array();
|
||||
|
||||
// Build variables array for formatter.
|
||||
$variables = array(
|
||||
'#obj_type' => $obj_type,
|
||||
'#object' => $object,
|
||||
'#field' => $field,
|
||||
'#instance' => $instance,
|
||||
'#langcode' => $langcode,
|
||||
'#items' => $items,
|
||||
'#display' => $display,
|
||||
);
|
||||
|
||||
if (function_exists($function = "{$display['module']}_field_formatter_{$display['type']}")) {
|
||||
$element[0] = array(
|
||||
'#markup' => $function($variables),
|
||||
);
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Field Formatter; <?php echo $item->label; ?>.
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_<?php echo $module; ?>_<?php echo $item->name; ?>($variables) {
|
||||
<?php foreach (explode("\n", $item->code) as $line) : ?>
|
||||
<?php echo $line . "\n"; ?>
|
||||
<?php endforeach; ?>
|
||||
}
|
135
sites/all/modules/custom_formatters/engines/php.inc
Normal file
135
sites/all/modules/custom_formatters/engines/php.inc
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Token engine for Custom Formatters modules.
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
function custom_formatters_engine_php_theme_alter(&$theme) {
|
||||
$theme['custom_formatters_php_export'] = array(
|
||||
'variables' => array(
|
||||
'item' => NULL,
|
||||
'module' => NULL,
|
||||
),
|
||||
'template' => 'php.export',
|
||||
'path' => drupal_get_path('module', 'custom_formatters') . '/engines',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings form callback for Custom Formatters PHP engine.
|
||||
*/
|
||||
function custom_formatters_engine_php_settings_form(&$form) {
|
||||
$form['code']['#description'] = t('Enter the PHP code that will be evaluated. You should NOT include %php tags. The $variables object is available.', array('%php' => '<?php ?>'));
|
||||
$form['code']['#attributes']['class'][] = 'syntax-php';
|
||||
|
||||
$form['settings'] = array(
|
||||
'#type' => 'container',
|
||||
);
|
||||
|
||||
// Additional debugging modes.
|
||||
$form['preview']['options']['dpm'] = array(
|
||||
'#type' => 'container',
|
||||
);
|
||||
$form['preview']['options']['dpm']['vars'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Output <strong>$variables</strong> array (requires <a href="http://drupal.org/project/devel">Devel</a> module).'),
|
||||
'#default_value' => module_exists('devel'),
|
||||
'#disabled' => !module_exists('devel'),
|
||||
);
|
||||
$form['preview']['options']['dpm']['html'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Output raw HTML (requires <a href="http://drupal.org/project/devel">Devel</a> module).'),
|
||||
'#default_value' => module_exists('devel'),
|
||||
'#disabled' => !module_exists('devel'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render callback for Custom Formatters PHP engine.
|
||||
*/
|
||||
function custom_formatters_engine_php_render($formatter, $obj_type, $object, $field, $instance, $langcode, $items, $display) {
|
||||
global $theme_path, $theme_info, $conf;
|
||||
|
||||
// Store current theme path.
|
||||
$old_theme_path = $theme_path;
|
||||
|
||||
// Restore theme_path to the theme, as long as php_eval() executes,
|
||||
// so code evaluated will not see the caller module as the current theme.
|
||||
// If theme info is not initialized get the path from theme_default.
|
||||
if (!isset($theme_info)) {
|
||||
$theme_path = drupal_get_path('theme', $conf['theme_default']);
|
||||
}
|
||||
else {
|
||||
$theme_path = dirname($theme_info->filename);
|
||||
}
|
||||
|
||||
// Build variables array for formatter.
|
||||
$variables = array(
|
||||
'#obj_type' => $obj_type,
|
||||
'#object' => $object,
|
||||
'#field' => $field,
|
||||
'#instance' => $instance,
|
||||
'#langcode' => $langcode,
|
||||
'#items' => $items,
|
||||
'#display' => $display,
|
||||
);
|
||||
|
||||
ob_start();
|
||||
$output = eval($formatter->code);
|
||||
$output = !empty($output) ? $output : ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
// Preview debugging; Show the available variables data.
|
||||
if (module_exists('devel') && isset($formatter->preview) && $formatter->preview['options']['dpm']['vars']) {
|
||||
dpm($variables);
|
||||
}
|
||||
|
||||
// Recover original theme path.
|
||||
$theme_path = $old_theme_path;
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export callback for Custom Formatters PHP engine.
|
||||
*/
|
||||
function custom_formatters_engine_php_export($item, $module) {
|
||||
$formatter = clone $item;
|
||||
|
||||
if (isset($formatter->fapi)) {
|
||||
ob_start();
|
||||
eval($formatter->fapi);
|
||||
ob_get_clean();
|
||||
|
||||
if (isset($form)) {
|
||||
$formatter->form = $form;
|
||||
}
|
||||
}
|
||||
|
||||
// Modify #default_value for hook_field_formatter_settings_form() export.
|
||||
include drupal_get_path('module', 'form_builder') . '/examples/form_builder_examples.module';
|
||||
foreach (element_children($form) as $key) {
|
||||
if (isset($form[$key]['#default_value'])) {
|
||||
unset($form[$key]['#default_value']);
|
||||
}
|
||||
$form[$key]['#default_value'] = "[default-value::{$key}]";
|
||||
}
|
||||
$formatter->fapi = form_builder_examples_export_recurse($form);
|
||||
preg_match_all('/\'\[default-value::(.*)?\]\'/', $formatter->fapi, $matches, PREG_SET_ORDER);
|
||||
foreach ($matches as $match) {
|
||||
$formatter->fapi = str_replace($match[0], "\$settings['{$match[1]}']", $formatter->fapi);
|
||||
}
|
||||
|
||||
return theme('custom_formatters_php_export', array('item' => $formatter, 'module' => $module));
|
||||
}
|
||||
|
||||
/**
|
||||
* Help callback for Custom Formatters PHP engine.
|
||||
*/
|
||||
function custom_formatters_engine_php_help() {
|
||||
return t('A PHP Formatter is essentially a standard Drupal API Field Formatter, the primary difference is that all availble parameters have been bundled into a <strong>$variables</strong> array for ease of use.') . "<br />\n"
|
||||
. t('The Formatter should be PHP code that returns either a value or <a href="http://drupal.org/node/930760">Render Array</a> and it should NOT include <em><?php ?></em> tags.') . "\n";
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
*/
|
||||
?>
|
||||
/**
|
||||
* Implements hook_field_formatter_info().
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_info() {
|
||||
return array(
|
||||
'<?php echo $module; ?>_<?php echo $item->name; ?>' => array(
|
||||
'label' => t('<?php echo $item->label; ?>'),
|
||||
'field types' => array('<?php echo implode('\', \'', drupal_explode_tags($item->field_types)); ?>'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_view().
|
||||
*/
|
||||
function <?php echo $module; ?>_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
|
||||
$element = array();
|
||||
$pattern = "<?php echo addslashes($item->code); ?>";
|
||||
|
||||
$info = token_get_info("{$field['type']}-field");
|
||||
$field_value = $info['field-value-type'];
|
||||
|
||||
$output = '';
|
||||
foreach ($items as $item) {
|
||||
$output .= token_replace($pattern, array(
|
||||
$obj_type => $object,
|
||||
$field_value => $item,
|
||||
'item' => $item,
|
||||
));
|
||||
}
|
||||
$element[0] = array(
|
||||
'#markup' => $output,
|
||||
);
|
||||
|
||||
return $element;
|
||||
}
|
81
sites/all/modules/custom_formatters/engines/token.inc
Normal file
81
sites/all/modules/custom_formatters/engines/token.inc
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Token engine for Custom Formatters modules.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements custom_formatters_engine_hook_theme_alter().
|
||||
*/
|
||||
function custom_formatters_engine_token_theme_alter(&$theme) {
|
||||
$theme['custom_formatters_token_export'] = array(
|
||||
'variables' => array(
|
||||
'item' => NULL,
|
||||
'module' => NULL,
|
||||
),
|
||||
'template' => 'token.export',
|
||||
'path' => drupal_get_path('module', 'custom_formatters') . '/engines',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings form callback for Custom Formatters Token engine.
|
||||
*/
|
||||
function custom_formatters_engine_token_settings_form(&$form, $form_state, $item) {
|
||||
$form['code']['#attributes']['class'][] = 'syntax-html';
|
||||
|
||||
// Additional debugging modes.
|
||||
$form['preview']['options']['dpm'] = array(
|
||||
'#type' => 'container',
|
||||
);
|
||||
$form['preview']['options']['dpm']['html'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Output raw HTML (requires !devel module).', array('!devel' => l(t('Devel'), 'http://drupal.org/project/devel'))),
|
||||
'#default_value' => module_exists('devel'),
|
||||
'#disabled' => !module_exists('devel'),
|
||||
);
|
||||
|
||||
$options = array();
|
||||
foreach (field_info_field_types() as $type => $field) {
|
||||
$options[$field['module']][$type] = $field['label'];
|
||||
}
|
||||
$form['field_types']['#type'] = 'select';
|
||||
$form['field_types']['#options'] = $options;
|
||||
unset($form['field_types']['#description']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render callback for Custom Formatters Token engine.
|
||||
*/
|
||||
function custom_formatters_engine_token_render($formatter, $obj_type, $object, $field, $instance, $langcode, $items, $display) {
|
||||
$element = array();
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$text = $formatter->code;
|
||||
$token_data = array(
|
||||
$obj_type => $object,
|
||||
'item' => $item,
|
||||
);
|
||||
drupal_alter('custom_formatters_token_data', $token_data, $text, $field);
|
||||
$element[$delta] = array(
|
||||
'#markup' => token_replace($text, $token_data, array('clear' => TRUE))
|
||||
);
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export callback for Custom Formatters Token engine.
|
||||
*/
|
||||
function custom_formatters_engine_token_export($item, $module) {
|
||||
return theme('custom_formatters_token_export', array('item' => $item, 'module' => $module));
|
||||
}
|
||||
|
||||
/**
|
||||
* Help callback for Custom Formatters Token engine.
|
||||
*/
|
||||
function custom_formatters_engine_token_help() {
|
||||
return t('A HTML + Token Formatter utilizes Drupal Tokens and the Token module to create easy yet powerful formatters.') . "<br />\n"
|
||||
. t('All available Tokens can be navigated via the Tokens fieldset displayed below the Formatter field.');
|
||||
}
|
Reference in New Issue
Block a user