custom_formatters.inc

  1. elmsmedia7 sites/all/modules/local_contrib/custom_formatters/includes/custom_formatters.inc
  2. elmsmedia7 sites/all/modules/local_contrib/custom_formatters/plugins/export_ui/custom_formatters.inc

CTools Export UI plugin for SexyBookmarks profiles.

Functions

File

sites/all/modules/local_contrib/custom_formatters/plugins/export_ui/custom_formatters.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * CTools Export UI plugin for SexyBookmarks profiles.
  5. */
  6. /**
  7. * CTools Export UI required function for plugin definition
  8. */
  9. function custom_formatters_custom_formatters_ctools_export_ui() {
  10. return array(
  11. 'schema' => 'formatters',
  12. 'access' => 'administer custom formatters',
  13. 'menu' => array(
  14. 'menu prefix' => 'admin/structure',
  15. 'menu item' => 'formatters',
  16. 'menu title' => 'Formatters',
  17. 'menu description' => 'Administer Formatters.',
  18. ),
  19. 'title singular' => t('formatter'),
  20. 'title singular proper' => t('Formatter'),
  21. 'title plural' => t('formatters'),
  22. 'title plural proper' => t('Formatters'),
  23. 'handler' => 'custom_formatters_ui',
  24. 'form' => array(
  25. 'settings' => 'custom_formatters_export_ui_form',
  26. 'submit' => 'custom_formatters_export_ui_form_submit',
  27. ),
  28. 'export' => array(
  29. 'admin_title' => 'label',
  30. 'admin_description' => 'description',
  31. ),
  32. );
  33. }
  34. /**
  35. * Custom Formatters settings form.
  36. */
  37. function custom_formatters_export_ui_form(&$form, &$form_state) {
  38. $form['#attached'] = array(
  39. 'css' => array(
  40. drupal_get_path('module', 'custom_formatters') . '/styles/custom_formatters.css',
  41. ),
  42. 'js' => array(
  43. drupal_get_path('module', 'custom_formatters') . '/scripts/custom_formatters.admin.js',
  44. ),
  45. );
  46. $item = $form_state['item'];
  47. $form['#item'] = $item;
  48. $form['#formatters'] = TRUE;
  49. $engines = module_invoke_all('custom_formatters_engine_info');
  50. $engine = !empty($form_state['values']['mode']) ? $form_state['values']['mode'] : (
  51. !empty($item->mode) ? $item->mode : key($engines)
  52. );
  53. if (isset($engines[$engine]['file']) && file_exists($engines[$engine]['file'])) {
  54. require_once $engines[$engine]['file'];
  55. }
  56. $disable = FALSE;
  57. if ($form_state['op'] != 'add') {
  58. $disable = custom_formatters_formatter_is_active($item);
  59. if ($disable) {
  60. drupal_set_message(t('As the Formatter is currently in use, certain features have been disabled.'), 'warning');
  61. }
  62. }
  63. $form['info']['#title'] = t('Basic information');
  64. $form['info']['#type'] = 'fieldset';
  65. $form['info']['#collapsible'] = TRUE;
  66. $form['info']['#collapsed'] = $form_state['op'] != 'add';
  67. $form['info']['label']['#title'] = t('Formatter name');
  68. $form['info']['description'] = array(
  69. '#title' => t('Description'),
  70. '#type' => 'textarea',
  71. '#default_value' => $item->description,
  72. );
  73. $options = array();
  74. foreach ($engines as $key => $option) {
  75. $options[$key] = $option['label'];
  76. }
  77. $form['info']['mode'] = array(
  78. '#title' => t('Format'),
  79. '#type' => 'select',
  80. '#options' => $options,
  81. '#default_value' => $engine,
  82. '#ajax' => array(
  83. 'callback' => 'custom_formatters_export_ui_form_js',
  84. 'wrapper' => 'engine-wrapper',
  85. ),
  86. );
  87. $form['engine'] = array(
  88. '#type' => 'container',
  89. '#prefix' => '<div id="engine-wrapper">',
  90. '#suffix' => '</div>'
  91. );
  92. $form['engine']['field_types'] = array(
  93. '#title' => t('Field type(s)'),
  94. '#type' => 'textfield',
  95. '#default_value' => $item->field_types,
  96. '#required' => TRUE,
  97. '#description' => t('Enter a comma-separated list of field types (file, image, text, text_long, etc).'),
  98. '#autocomplete_path' => 'js/formatters/autocomplete',
  99. '#ajax' => array(
  100. 'callback' => 'custom_formatters_export_ui_form_js',
  101. 'wrapper' => 'engine-wrapper',
  102. ),
  103. '#disabled' => $disable,
  104. );
  105. $form['engine']['code'] = array(
  106. '#title' => t('Formatter'),
  107. '#type' => 'textarea',
  108. '#default_value' => !empty($item->code) ? $item->code : '',
  109. '#required' => TRUE,
  110. '#rows' => 10,
  111. );
  112. $options = _custom_formatters_preview_variables($form, $form_state);
  113. $form['engine']['preview'] = array(
  114. '#title' => t('Preview'),
  115. '#type' => 'fieldset',
  116. '#collapsible' => TRUE,
  117. '#collapsed' => TRUE,
  118. '#weight' => 100,
  119. '#theme' => 'custom_formatters_export_ui_form_preview',
  120. '#tree' => TRUE,
  121. );
  122. $form['engine']['preview']['entity_type'] = array(
  123. '#title' => t('Entity type'),
  124. '#type' => 'select',
  125. '#options' => $options['entity_types'],
  126. '#default_value' => 'node',
  127. '#disabled' => empty($options['entity_types']),
  128. '#ajax' => array(
  129. 'callback' => 'custom_formatters_export_ui_form_js',
  130. 'wrapper' => 'engine-wrapper',
  131. ),
  132. );
  133. $form['engine']['preview']['bundle'] = array(
  134. '#title' => t('Bundle'),
  135. '#type' => 'select',
  136. '#options' => $options['bundles'],
  137. '#disabled' => empty($options['bundles']),
  138. '#ajax' => array(
  139. 'callback' => 'custom_formatters_export_ui_form_js',
  140. 'wrapper' => 'engine-wrapper',
  141. ),
  142. );
  143. $form['engine']['preview']['field'] = array(
  144. '#title' => t('Field'),
  145. '#type' => 'select',
  146. '#options' => $options['fields'],
  147. '#disabled' => empty($options['fields']),
  148. '#ajax' => array(
  149. 'callback' => 'custom_formatters_export_ui_form_js',
  150. 'wrapper' => 'engine-wrapper',
  151. ),
  152. );
  153. $form['engine']['preview']['entity'] = array(
  154. '#title' => t('Entity'),
  155. '#type' => 'select',
  156. '#options' => $options['entities'],
  157. '#disabled' => empty($options['entities']),
  158. );
  159. $form['engine']['preview']['options'] = array(
  160. '#type' => 'container',
  161. );
  162. $form['engine']['preview']['button'] = array(
  163. '#type' => 'button',
  164. '#value' => t('Preview'),
  165. '#ajax' => array(
  166. 'callback' => 'custom_formatters_export_ui_form_js_preview',
  167. 'wrapper' => 'preview-wrapper',
  168. ),
  169. '#disabled' => empty($options['entities']),
  170. );
  171. $form['engine']['preview']['preview'] = array(
  172. '#type' => 'container',
  173. '#prefix' => '<div id="preview-wrapper">',
  174. '#suffix' => '</div>',
  175. );
  176. $engines[$engine]['callbacks']['settings form']($form['engine'], $form_state, $item);
  177. $form['buttons']['edit'] = array(
  178. '#type' => 'submit',
  179. '#value' => t('Save & Edit'),
  180. );
  181. }
  182. function _custom_formatters_preview_variables($form, $form_state) {
  183. $options = array();
  184. $item = !empty($form_state['values']) ? $form_state['values'] : (array) $form_state['item'];
  185. // Entity Types
  186. $options['entity_types'] = array();
  187. $entity_types = entity_get_info();
  188. foreach ($entity_types as $key => $entity) {
  189. if ($entity['fieldable']) {
  190. $options['entity_types'][$key] = $entity['label'];
  191. }
  192. }
  193. // Bundles
  194. $options['bundles'] = array();
  195. if (!empty($options['entity_types'])) {
  196. $entity_type = !empty($item['preview']['entity_type']) ? $item['preview']['entity_type'] : 'node';
  197. foreach ($entity_types[$entity_type]['bundles'] as $key => $bundle) {
  198. $options['bundles'][$key] = $bundle['label'];
  199. }
  200. }
  201. // Fields
  202. $options['fields'] = array();
  203. if (!empty($options['bundles'])) {
  204. $bundle = !empty($item['preview']['bundle']) && isset($options['bundles'][$item['preview']['bundle']])
  205. ? $item['preview']['bundle'] : key($options['bundles']);
  206. $fields = field_info_instances($entity_type, $bundle);
  207. foreach ($fields as $key => $field) {
  208. $field_info = field_info_field_by_id($field['field_id']);
  209. if (in_array($field_info['type'], drupal_explode_tags($item['field_types']))) {
  210. $options['fields'][$key] = $field['label'];
  211. }
  212. }
  213. }
  214. // Entities
  215. $options['entities'] = array();
  216. if (!empty($options['fields'])) {
  217. $field = !empty($item['preview']['field']) ? $item['preview']['field'] : key($options['fields']);
  218. if (strstr($field, 'field_')) {
  219. $query = new EntityFieldQuery();
  220. $query->entityCondition('entity_type', $entity_type)
  221. ->entityCondition('bundle', $bundle)
  222. ->addTag('random')
  223. ->range(0, 50)
  224. ->fieldCondition($field);
  225. $result = $query->execute();
  226. if (!empty($result)) {
  227. $entities = entity_load($entity_type, array_keys($result[$entity_type]));
  228. foreach ($entities as $key => $entity) {
  229. $options['entities'][$key] = entity_label($entity_type, $entity) . " [eid:{$key}]";
  230. }
  231. }
  232. }
  233. }
  234. return $options;
  235. }
  236. /**
  237. * Implements hook_query_TAG_alter().
  238. */
  239. function custom_formatters_query_random_alter($query) {
  240. $query->orderRandom();
  241. }
  242. function custom_formatters_export_ui_form_js($form, $form_state) {
  243. // Clear messages.
  244. drupal_get_messages(NULL, TRUE);
  245. $engines = module_invoke_all('custom_formatters_engine_info');
  246. $engine = $form_state['values']['mode'];
  247. if (isset($engines[$engine]['file']) && file_exists($engines[$engine]['file'])) {
  248. require_once $engines[$engine]['file'];
  249. }
  250. return $form['engine'];
  251. }
  252. /**
  253. *
  254. */
  255. function custom_formatters_export_ui_form_js_preview($form, $form_state, $object = NULL) {
  256. // Clear messages.
  257. drupal_get_messages(NULL, TRUE);
  258. // Build preview elements.
  259. $obj_type = $form_state['values']['preview']['entity_type'];
  260. if (is_null($object)) {
  261. $objects = entity_load($obj_type, array($form_state['values']['preview']['entity']));
  262. $object = $objects[$form_state['values']['preview']['entity']];
  263. }
  264. $field = field_info_field($form_state['values']['preview']['field']);
  265. $instance = field_info_instance($obj_type, $field['field_name'], $form_state['values']['preview']['bundle']);
  266. $langcode = field_language($obj_type, $object, $field['field_name']);
  267. $items = field_get_items($obj_type, $object, $instance['field_name'], $langcode);
  268. // Prepare field view.
  269. $functions = array("{$field['module']}_field_prepare_view", "{$field['module']}_field_load");
  270. foreach ($functions as $function) {
  271. if (function_exists($function)) {
  272. $items = array($items);
  273. $function($entity_type, array($entity), $field, array($instance), $langcode, $items, FIELD_LOAD_CURRENT);
  274. $items = $items[0];
  275. }
  276. }
  277. $display = $instance['display']['default'];
  278. // @TODO - Allow this to be done via form_builder.inc or any other Foramtter
  279. // Settings integration modules.
  280. $display['settings'] = isset($form_state['input']['settings']['preview']) ? $form_state['input']['settings']['preview'] : $display['settings'];
  281. // Disable contextual links in preview.
  282. $display['#cf_options'] = array('#contextual_links' => FALSE);
  283. $formatter = (object) $form_state['values'];
  284. // Build the preview html.
  285. $element = custom_formatters_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display, $formatter);
  286. $variables = array(
  287. 'element' => array_merge($element, array(
  288. '#label_display' => 'hidden',
  289. '#title' => NULL,
  290. '#items' => $element,
  291. '#field_name' => $field['field_name'],
  292. '#field_type' => $field['type']
  293. )),
  294. );
  295. foreach (array_keys($element) as $delta) {
  296. $variables['item_attributes'][$delta] = NULL;
  297. }
  298. $js = drupal_add_js();
  299. $form['engine']['preview']['preview']['content'] = array(
  300. '#markup' => theme('field', $variables),
  301. );
  302. $js = array_diff_assoc(drupal_add_js(), $js);
  303. $form['engine']['preview']['preview']['content']['#markup'] .= drupal_get_js('header', $js);
  304. // Preview debugging; Show the output HTML.
  305. if (module_exists('devel') && isset($formatter->preview) && $formatter->preview['options']['dpm']['html']) {
  306. dpm(theme('field', $variables));
  307. }
  308. return $form['engine']['preview']['preview'];
  309. }
  310. /**
  311. * Submit callback for Custom Formatters settings form.
  312. */
  313. function custom_formatters_export_ui_form_submit(&$form, &$form_state) {
  314. field_cache_clear();
  315. if ($form_state['values']['op'] == t('Save & Edit')) {
  316. $destination = "admin/structure/formatters/list/{$form_state['values']['name']}/edit";
  317. $_GET['destination'] = isset($_GET['destination']) ? $destination . "?destination={$_GET['destination']}" : $destination;
  318. }
  319. }
  320. /**
  321. * Provide a form for displaying an export.
  322. */
  323. function custom_formatters_export_ui_export_form($form, &$form_state, $item, $title = '') {
  324. $form['mode'] = array(
  325. '#type' => 'select',
  326. '#title' => t('Mode'),
  327. '#options' => array(
  328. 'default' => t('CTools exportable (default)'),
  329. 'drupal' => t('Drupal API')
  330. ),
  331. '#default_value' => 'default',
  332. '#ajax' => array(
  333. 'callback' => 'custom_formatters_export_ui_export_form_js',
  334. 'wrapper' => 'export-wrapper',
  335. ),
  336. );
  337. $form['export'] = array(
  338. '#type' => 'container',
  339. '#prefix' => '<div id="export-wrapper">',
  340. '#suffix' => '</div>',
  341. );
  342. $mode = isset($form_state['values']['mode']) ? $form_state['values']['mode'] : $form['mode']['#default_value'];
  343. switch ($mode) {
  344. case 'default':
  345. $code = ctools_export_crud_export('formatters', $item);
  346. break;
  347. case 'drupal':
  348. $engines = module_invoke_all('custom_formatters_engine_info');
  349. $engine = $item->mode;
  350. if (isset($engines[$engine]['file']) && file_exists($engines[$engine]['file'])) {
  351. require_once $engines[$engine]['file'];
  352. }
  353. $module = isset($form_state['values']['module']) ? $form_state['values']['module'] : t('MYMODULE');
  354. $form['export']['module'] = array(
  355. '#type' => 'textfield',
  356. '#title' => t('Module name'),
  357. '#default_value' => $module,
  358. '#ajax' => array(
  359. 'callback' => 'custom_formatters_export_ui_export_form_js',
  360. 'wrapper' => 'export-wrapper',
  361. ),
  362. );
  363. $code = $engines[$engine]['callbacks']['export']($item, $module);
  364. break;
  365. }
  366. $lines = substr_count($code, "\n");
  367. $form['export']['code'] = array(
  368. '#type' => 'textarea',
  369. '#title' => check_plain($title),
  370. '#value' => $code,
  371. '#rows' => $lines,
  372. );
  373. return $form;
  374. }
  375. function custom_formatters_export_ui_export_form_js($form, $form_state) {
  376. return $form['export'];
  377. }
  378. /**
  379. * Field Types autocomplete callback.
  380. */
  381. function custom_formatters_autocomplete($string = '') {
  382. // The user enters a comma-separated list of fields. We only autocomplete the last tag.
  383. $array = drupal_explode_tags($string);
  384. // Fetch last field.
  385. $last_string = trim(array_pop($array));
  386. $matches = array();
  387. if ($last_string != '') {
  388. $prefix = count($array) ? implode(', ', $array) . ', ' : '';
  389. $fields = array_keys(field_info_field_types());
  390. // Give modules a chance to alter fields.
  391. //drupal_alter('custom_formatters_fields', $fields);
  392. sort($fields);
  393. foreach ($fields as $field) {
  394. if (preg_match('/^' . drupal_strtolower($last_string) . '/', $field)) {
  395. $matches[$prefix . $field] = check_plain($field);
  396. }
  397. }
  398. }
  399. drupal_json_output($matches);
  400. }
  401. /**
  402. * Theme for Preview form.
  403. */
  404. function theme_custom_formatters_export_ui_form_preview($variable) {
  405. $form = $variable['form'];
  406. $output = '';
  407. $output .= theme('table', array(
  408. 'rows' => array(
  409. array(
  410. render($form['entity_type']),
  411. render($form['bundle']),
  412. render($form['field']),
  413. render($form['entity']),
  414. ),
  415. array(
  416. array('data' => render($form['options']), 'colspan' => 3),
  417. render($form['button'])
  418. ),
  419. ),
  420. ));
  421. $output .= render($form['preview']);
  422. return $output;
  423. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.