field_collection_tabs_widget.module

  1. cis7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  2. cle7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  3. ecd7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  4. elmsmedia7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  5. harmony7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  6. icor7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  7. meedjum_blog7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
  8. mooc7 sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module

Provides widget for field collections on tabs

Functions

File

sites/all/modules/ulmus/field_collection_tabs_widget/field_collection_tabs_widget.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides widget for field collections on tabs
  5. */
  6. /**
  7. * Implements hook_field_formatter_info().
  8. */
  9. function field_collection_tabs_widget_field_widget_info() {
  10. return array(
  11. 'field_collection_tabs' => array(
  12. 'label' => t('Tabs'),
  13. 'description' => t('Field collection form elements as tabs'),
  14. 'field types' => array(
  15. 'field_collection'
  16. ),
  17. 'settings' => array(
  18. 'fctw_title_field' => 0,
  19. 'fctw_title_field_formatter' => 'default',
  20. 'fctw_new_tab_title' => t('New'),
  21. 'fctw_empty_tab_title' => t('- None -'),
  22. 'fctw_add_new_tab_title' => t('+'),
  23. ),
  24. 'behaviors' => array(
  25. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  26. 'default value' => FIELD_BEHAVIOR_NONE,
  27. ),
  28. ),
  29. );
  30. }
  31. /**
  32. * Implements hook_field_formatter_settings_form().
  33. */
  34. function field_collection_tabs_widget_field_widget_settings_form($field, $instance) {
  35. // The field settings infrastructure is not AJAX enabled by default,
  36. // because it doesn't pass over the $form_state.
  37. // Build the whole form into a #process in which we actually have access
  38. // to the form state. Idea stolen from entityreference.
  39. $form['settings'] = array(
  40. '#type' => 'container',
  41. '#tree' => TRUE,
  42. '#process' => array(
  43. '_fctw_form_process_merge_parent',
  44. '_fctw_widget_settings_process'
  45. ),
  46. '#element_validate' => array('_fctw_widget_settings_validate'),
  47. '#field' => $field,
  48. '#instance' => $instance,
  49. );
  50. return $form;
  51. }
  52. /**
  53. * Widget settings form process callback.
  54. */
  55. function _fctw_form_process_merge_parent($element) {
  56. $parents = $element['#parents'];
  57. array_pop($parents);
  58. $element['#parents'] = $parents;
  59. return $element;
  60. }
  61. /**
  62. * Widget settings form process callback.
  63. */
  64. function _fctw_widget_settings_process($element, $form_state) {
  65. $field = $element['#field'];
  66. $instance = $element['#instance'];
  67. $settings = $instance['widget']['settings'];
  68. $element['fctw_new_tab_title'] = array(
  69. '#type' => 'textfield',
  70. '#title' => t('Text to use on a new tab'),
  71. '#default_value' => $settings['fctw_new_tab_title'],
  72. '#size' => 10,
  73. '#maxlength' => 30,
  74. );
  75. $element['fctw_add_new_tab_title'] = array(
  76. '#type' => 'textfield',
  77. '#title' => t('Text to use on the "add a new tab" tab'),
  78. '#default_value' => $settings['fctw_add_new_tab_title'],
  79. '#size' => 10,
  80. '#maxlength' => 30,
  81. );
  82. $options = array(t('No titles'));
  83. $fields = field_info_instances('field_collection_item', $field['field_name']);
  84. foreach ($fields as $field_name => $field) {
  85. $options[$field_name] = $field['label'];
  86. }
  87. $id = 'fctw-title-field-formatter';
  88. $element['fctw_title_field'] = array(
  89. '#type' => 'select',
  90. '#title' => t('Field to use for tab titles'),
  91. '#default_value' => $settings['fctw_title_field'],
  92. '#options' => $options,
  93. '#ajax' => array(
  94. 'callback' => 'fctw_title_field_ajax_callback',
  95. 'wrapper' => $id,
  96. 'element' => $element['#array_parents'],
  97. ),
  98. );
  99. $element['fctw_empty_tab_title'] = array(
  100. '#type' => 'textfield',
  101. '#title' => t('Text to use if the field is empty'),
  102. '#default_value' => $settings['fctw_empty_tab_title'],
  103. '#size' => 10,
  104. '#maxlength' => 30,
  105. );
  106. $element['fctw_title_field']['#ajax']['element'][] = 'fctw_title_field_formatter';
  107. if (isset($form_state['input']['_triggering_element_name']) && ($form_state['input']['_triggering_element_name'] == 'instance[widget][settings][fctw_title_field]')) {
  108. $title_field = $form_state['input']['instance']['widget']['settings']['fctw_title_field'];
  109. }
  110. else {
  111. $title_field = $element['fctw_title_field']['#default_value'];
  112. }
  113. if ($title_field) {
  114. $field_type = field_info_field($title_field);
  115. $field_type = $field_type['type'];
  116. $formatter_options = field_ui_formatter_options($field_type);
  117. $element['fctw_title_field_formatter'] = array(
  118. '#type' => 'select',
  119. '#title' => t('Formatter'),
  120. '#options' => $formatter_options,
  121. '#prefix' => '<div id="' . $id . '">',
  122. '#suffix' => '</div>',
  123. );
  124. if (empty($form_state['input']['_triggering_element_name']) && empty($form_state['triggering_element'])) {
  125. $element['fctw_title_field_formatter']['#default_value'] = $settings['fctw_title_field_formatter'];
  126. }
  127. // We are in an AJAX call: the select #options change and therefore
  128. // the #value needs to be updated.
  129. else {
  130. $keys = array_keys($formatter_options);
  131. $element['fctw_title_field_formatter']['#default_value'] = reset($keys);
  132. $element['fctw_title_field_formatter']['#value'] = reset($keys);
  133. }
  134. }
  135. else {
  136. $element['fctw_title_field_formatter'] = array(
  137. '#prefix' => '<div id="' . $id . '">',
  138. '#suffix' => '</div>',
  139. );
  140. }
  141. return $element;
  142. }
  143. /**
  144. * AJAX callback for the widget settings form.
  145. */
  146. function fctw_title_field_ajax_callback($form, $form_state) {
  147. $trigger = $form_state['triggering_element'];
  148. $key_exists = NULL;
  149. $return = drupal_array_get_nested_value($form, $trigger['#ajax']['element'], $key_exists);
  150. if ($key_exists) {
  151. return $return;
  152. }
  153. else {
  154. array_shift($trigger['#ajax']['element']);
  155. return drupal_array_get_nested_value($form, $trigger['#ajax']['element'], $key_exists);
  156. }
  157. }
  158. /**
  159. * Validate callback for the widget settings form.
  160. */
  161. function _fctw_widget_settings_validate($form, &$form_state) {
  162. if (!isset($form_state['values']['instance']['widget']['settings']['fctw_title_field_formatter']) && isset($form_state['input']['instance']['widget']['settings']['fctw_title_field_formatter'])) {
  163. $form_state['values']['instance']['widget']['settings']['fctw_title_field_formatter'] = $form_state['input']['instance']['widget']['settings']['fctw_title_field_formatter'];
  164. }
  165. }
  166. /**
  167. * Implements hook_field_widget_form().
  168. */
  169. function field_collection_tabs_widget_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  170. $instance['widget']['type'] = 'field_collection_embed';
  171. $element = field_collection_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
  172. return $element;
  173. }
  174. /**
  175. * Implements hook_theme_registry_alter().
  176. *
  177. * We want our own version of field_multiple_value_form().
  178. */
  179. function field_collection_tabs_widget_theme_registry_alter(&$theme_registry) {
  180. $theme_registry['field_multiple_value_form']['function'] = 'theme_field_collection_tabs_widget_field_multiple_value_form';
  181. }
  182. /**
  183. * Implements hook_theme().
  184. */
  185. function field_collection_tabs_widget_theme($existing, $type, $theme, $path) {
  186. return array(
  187. 'fctw_item_list' => array(
  188. 'variables' => array(
  189. 'items' => array(),
  190. 'title' => NULL,
  191. 'type' => 'ul',
  192. 'attributes' => array()
  193. ),
  194. ),
  195. 'fctw_add_new_tab' => array(
  196. 'variables' => array(
  197. 'instance' => array(),
  198. ),
  199. ),
  200. );
  201. }
  202. /**
  203. * Implements hook_field_widget_form_alter().
  204. */
  205. function field_collection_tabs_widget_field_widget_form_alter(&$element, &$form_state, $context) {
  206. $element['#instance'] = $context['instance'];
  207. }
  208. /**
  209. * Implements hook_libraries_info().
  210. */
  211. function field_collection_tabs_widget_libraries_info() {
  212. $library_path = libraries_get_path('erta');
  213. $libraries['erta'] = array(
  214. 'name' => 'Easy Responsve Tabs to Accordion plugin',
  215. 'vendor url' => 'https://github.com/samsono/Easy-Responsive-Tabs-to-Accordion',
  216. 'download url' => 'https://github.com/samsono/Easy-Responsive-Tabs-to-Accordion/archive/master.zip',
  217. 'version arguments' => array(
  218. 'file' => 'Easy-Responsive-Tabs-to-Accordion.jquery.json',
  219. 'pattern' => '@"version": "([0-9\.a-z]+)"@',
  220. 'lines' => 5,
  221. ),
  222. 'files' => array(
  223. 'js' => array(
  224. 'js/easyResponsiveTabs.js',
  225. ),
  226. 'css' => array(
  227. 'css/easy-responsive-tabs.css',
  228. ),
  229. ),
  230. );
  231. return $libraries;
  232. }
  233. /**
  234. * Implements hook_requirements().
  235. */
  236. function field_collection_tabs_widget_requirements($phase) {
  237. $t = get_t();
  238. $requirements = array();
  239. $requirements['erta'] = array(
  240. 'title' => $t('Easy Responsive Tabs to Accordion version'),
  241. );
  242. $erta = libraries_load('erta');
  243. if (!$erta['installed']) {
  244. $requirements['erta']['value'] = $t('Not installed');
  245. $requirements['erta']['description'] = $t('Easy Responsive Tabs to Accordion is not installed.');
  246. $requirements['erta']['severity'] = REQUIREMENT_ERROR;
  247. }
  248. elseif (version_compare($erta['version'], '1.2.1') < 0) {
  249. $requirements['erta']['value'] = $erta['version'];
  250. $requirements['erta']['description'] = $t('Easy Responsive Tabs to Accordion version is lower than 1.2.1.');
  251. $requirements['erta']['severity'] = REQUIREMENT_ERROR;
  252. }
  253. else {
  254. $requirements['erta']['value'] = $erta['version'];
  255. }
  256. return $requirements;
  257. }
  258. /**
  259. * Theme function for generating an item list.
  260. *
  261. * Completely identical to theme_item_list() apart from the
  262. * item-list wrapper.
  263. */
  264. function theme_fctw_item_list($variables) {
  265. $items = $variables['items'];
  266. $title = $variables['title'];
  267. $type = $variables['type'];
  268. $attributes = $variables['attributes'];
  269. $output = '';
  270. // Only output the list container and title, if there are any list items.
  271. // Check to see whether the block title exists before adding a header.
  272. // Empty headers are not semantic and present accessibility challenges.
  273. if (isset($title) && $title !== '') {
  274. $output .= '<h3>' . $title . '</h3>';
  275. }
  276. if (!empty($items)) {
  277. $output .= "<$type" . drupal_attributes($attributes) . '>';
  278. $num_items = count($items);
  279. $i = 0;
  280. foreach ($items as $item) {
  281. $attributes = array();
  282. $children = array();
  283. $data = '';
  284. $i++;
  285. if (is_array($item)) {
  286. foreach ($item as $key => $value) {
  287. if ($key == 'data') {
  288. $data = $value;
  289. }
  290. elseif ($key == 'children') {
  291. $children = $value;
  292. }
  293. else {
  294. $attributes[$key] = $value;
  295. }
  296. }
  297. }
  298. else {
  299. $data = $item;
  300. }
  301. if (count($children) > 0) {
  302. // Render nested list.
  303. $data .= theme_item_list(array('items' => $children, 'title' => NULL, 'type' => $type, 'attributes' => $attributes));
  304. }
  305. if ($i == 1) {
  306. $attributes['class'][] = 'first';
  307. }
  308. if ($i == $num_items) {
  309. $attributes['class'][] = 'last';
  310. }
  311. $output .= '<li' . drupal_attributes($attributes) . '>' . $data . "</li>\n";
  312. }
  313. $output .= "</$type>";
  314. }
  315. return $output;
  316. }
  317. /**
  318. * Replacement for theme_field_multiple_value_form().
  319. */
  320. function theme_field_collection_tabs_widget_field_multiple_value_form($variables) {
  321. $element = $variables['element'];
  322. $instance = $element['0']['#instance'];
  323. $output = '';
  324. if ($instance['widget']['type'] == 'field_collection_tabs') {
  325. if (isset($element['#cardinality']) && ($element['#cardinality'] > 1 || $element['#cardinality'] == FIELD_CARDINALITY_UNLIMITED)) {
  326. $title_field = $instance['widget']['settings']['fctw_title_field'];
  327. $id = drupal_html_id($element['#field_name'] . '_values');
  328. $required = !empty($element['#required']) ? '<span class="form-required" title="' . t('This field is required. ') . '">*</span>' : '';
  329. $resp_tabs_list = array();
  330. $resp_tabs_container = array();
  331. // Sort items according to '_weight' (needed when the form comes back after
  332. // preview or failed validation)
  333. $items = array();
  334. foreach (element_children($element) as $key) {
  335. if ($key === 'add_more') {
  336. $add_more_button = &$element[$key];
  337. }
  338. else {
  339. $items[] = &$element[$key];
  340. }
  341. }
  342. usort($items, '_field_sort_items_value_helper');
  343. // Add the items as list rows.
  344. foreach ($items as $key => $item) {
  345. if (!isset($item['#entity'])) {
  346. continue;
  347. }
  348. $title = field_get_items('field_collection_item', $item['#entity'], $title_field);
  349. // This is a new field collection
  350. if (!isset($title) || empty($item['#entity']->item_id)) {
  351. $title = $instance['widget']['settings']['fctw_new_tab_title'];
  352. }
  353. // The title field is set, let's use it.
  354. else {
  355. $display = array(
  356. 'label' => 'hidden',
  357. 'type' => $instance['widget']['settings']['fctw_title_field_formatter'],
  358. );
  359. $title = field_view_field($item['#entity_type'], $item['#entity'], $title_field, $display);
  360. $title = drupal_render($title);
  361. }
  362. // If the title field is empty use the default text.
  363. if (empty($title)) {
  364. $title = $instance['widget']['settings']['fctw_empty_tab_title'];
  365. }
  366. // Tab text
  367. $resp_tabs_list[] = $title;
  368. // Tab contents
  369. $item['_weight']['#attributes']['class'][] = 'fctw-weight';
  370. $resp_tabs_container[] = array(
  371. 'fc_form' => array(
  372. '#type' => 'container',
  373. '#attributes' => array(),
  374. 'item' => $item,
  375. ),
  376. );
  377. }
  378. // Adding the "Add a new tab" tab.
  379. $resp_tabs_list[] = theme('fctw_add_new_tab', array('instance' => $instance));
  380. $resp_tabs_container[] = array(
  381. 'fc_form' => array(
  382. '#type' => 'container',
  383. '#attributes' => array(),
  384. 'item' => $add_more_button,
  385. ),
  386. );
  387. // We add the id of the whole fc widget to Drupal.settings to
  388. // call easyResponsiveTabs() on them.
  389. $id = drupal_html_id('isj-shared-files');
  390. drupal_add_js(array('fieldCollectionTabsWidgetIds' => array($id)), 'setting');
  391. // Adding necessary javascript to do the magic.
  392. libraries_load('erta');
  393. drupal_add_library('system', 'ui.sortable');
  394. drupal_add_js(drupal_get_path('module', 'field_collection_tabs_widget') . '/field_collection_tabs_widget.js');
  395. drupal_add_css(drupal_get_path('module', 'field_collection_tabs_widget') . '/field_collection_tabs_widget.css');
  396. $output = array(
  397. '#type' => 'container',
  398. '#attributes' => array(
  399. 'id' => $id,
  400. ),
  401. 'tabs' => array(
  402. '#theme' => 'fctw_item_list',
  403. '#type' => 'ul',
  404. '#items' => $resp_tabs_list,
  405. '#attributes' => array(
  406. 'class' => array('resp-tabs-list'),
  407. ),
  408. ),
  409. 'resp_tabs_container' => array(
  410. '#type' => 'container',
  411. '#attributes' => array(
  412. 'class' => array('resp-tabs-container'),
  413. ),
  414. 'rtc' => $resp_tabs_container,
  415. ),
  416. );
  417. $output = drupal_render($output);
  418. }
  419. else {
  420. foreach (element_children($element) as $key) {
  421. $output .= drupal_render($element[$key]);
  422. }
  423. }
  424. }
  425. else {
  426. // Cannot use theme() here because of field_collection_tabs_widget_theme_registry_alter().
  427. return theme_field_multiple_value_form($variables);
  428. }
  429. return $output;
  430. }
  431. /**
  432. * Theme function to theme the "Add a new tab" tab.
  433. */
  434. function theme_fctw_add_new_tab(&$variables) {
  435. $instance = $variables['instance'];
  436. return '<div class="add-more_button" title="' . t('Add a new tab') . '">' . $instance['widget']['settings']['fctw_add_new_tab_title'] . '</div>';
  437. }
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.