You are here

function theme_custom_formatters_preview in Custom Formatters 6

2 theme calls to theme_custom_formatters_preview()
custom_formatters_formatter_form in ./custom_formatters.admin.inc
custom_formatters_preview in ./custom_formatters.admin.inc
Menu callback; Retrieve a JSON object containing Custom Formatter preview.

File

./custom_formatters.admin.inc, line 897
Contains administration functions for the Custom Formatters module.

Code

function theme_custom_formatters_preview($formatter) {
  module_load_include('inc', 'devel_generate');
  include_once drupal_get_path('module', 'content') . '/includes/content.devel.inc';
  $output = '';

  // Build temporary node object.
  $node = new stdClass();
  $node->type = isset($formatter->node) ? $formatter->node : array_shift(array_keys(devel_generate_content_types()));
  module_load_include('inc', 'node', 'node.pages');
  node_object_prepare($node);
  devel_generate_content_pre_node($results);
  $users = $results['users'];
  $node->nid = rand(1, 3);
  $node->uid = $users[array_rand($users)];
  $node->title = devel_create_greeking(mt_rand(1, 8), TRUE);
  $node->body = "node ({$node->type}) - " . devel_create_content();
  $node->teaser = node_teaser($node->body);
  $node->filter = variable_get('filter_default_format', 1);
  $node->format = FILTER_FORMAT_DEFAULT;
  $node->language = '';
  $node->revision = mt_rand(0, 1);
  $node->promote = mt_rand(0, 1);
  $node->created = time() - mt_rand(0, 604800);
  $info = content_types($node->type);
  $fields = _content_field_types();

  // Give modules a change to alter fields.
  drupal_alter('custom_formatters_fields', $fields);
  if (isset($formatter) && !empty($formatter)) {
    foreach (unserialize($formatter->field_types) as $field_type) {
      if (isset($fields[$field_type])) {
        $module = $fields[$field_type]['module'];
        $field = array(
          'field_name' => NULL,
          'type' => $field_type,
          'multiple' => 1,
          'widget' => array(
            'module' => '_custom_formatters',
            'type' => isset($formatter->multiple) && $formatter->multiple ? 'multiple' : 'single',
          ),
        );

        // Invoke hook_custom_formatters_field_prepare().
        if (function_exists($function = "{$module}_custom_formatters_{$field_type}_prepare")) {
          $field = _custom_formatters_array_merge_recursive($function(), $field);
        }
        $items = array();

        // Invoke hook_content_generate().
        if (function_exists($function = "{$module}_content_generate")) {
          $items = $function((array) $node, $field);
          foreach ($info['fields'] as $key => $node_field) {
            if ($node_field['type'] == $field['type']) {
              $node->{$key} = array(
                $items,
              );
            }
          }
        }
        $element = array(
          'items' => array(),
          '#single' => FALSE,
          '#title' => $fields[$field_type]['label'],
          '#page' => TRUE,
          '#field_name' => 'field_devel_generate',
          '#label_display' => 'above',
          '#teaser' => FALSE,
          '#node' => $node,
          '#type' => 'content_field',
          '#custom_formatters' => TRUE,
        );
        if (!isset($formatter->multiple) || !$formatter->multiple) {
          $element['items'] = array(
            '#formatter' => $formatter,
            '#node' => $node,
            '#type_name' => $node->type,
            '#field_name' => 'field_devel_generate',
            '#theme' => 'custom_formatters_formatter',
            '#item' => $items,
            '#devel' => TRUE,
            '#field_type' => $field_type,
          );
        }
        else {
          $element['items'] = array(
            '#formatter' => $formatter,
            '#node' => $node,
            '#type_name' => $node->type,
            '#field_name' => 'field_devel_generate',
            '#theme' => 'custom_formatters_formatter',
            '#node' => $node,
            '#devel' => TRUE,
            '#field_type' => $field_type,
          );
          foreach ($items as $item) {
            $element['items'][] = array(
              '#item' => $item,
            );
          }
        }
        $output .= drupal_render($element);
      }
      else {

        // Field type doesn't exist.
        drupal_set_message(t("Field type %field_type does not exist or it's parent module is currently disabled.", array(
          '%field_type' => $field_type,
        )), 'warning');
      }
    }
  }
  return $output;
}