field_collection.pages.inc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @file
  4. * Provides the field collection item view / edit / delete pages.
  5. */
  6. // TODO: fix being embedded in a host with revisions.
  7. /**
  8. * Field collection item view page.
  9. */
  10. function field_collection_item_page_view($field_collection_item) {
  11. // @todo: Set breadcrumb including the host.
  12. drupal_set_title($field_collection_item->label());
  13. return $field_collection_item->view('full', NULL, TRUE);
  14. }
  15. /**
  16. * Form for editing a field collection item.
  17. * @todo implement hook_forms().
  18. */
  19. function field_collection_item_form($form, &$form_state, $field_collection_item) {
  20. if (!isset($field_collection_item->is_new)) {
  21. drupal_set_title($field_collection_item->label());
  22. }
  23. $form_state += array('field_collection_item' => $field_collection_item);
  24. // Hack: entity_form_field_validate() needs the bundle to be set.
  25. // @todo: Fix core and remove the hack.
  26. $form['field_name'] = array('#type' => 'value', '#value' => $field_collection_item->field_name);
  27. $langcode = entity_language('field_collection_item', $field_collection_item);
  28. field_attach_form('field_collection_item', $field_collection_item, $form, $form_state, $langcode);
  29. $form['actions'] = array('#type' => 'actions', '#weight' => 50);
  30. $form['actions']['submit'] = array(
  31. '#type' => 'submit',
  32. '#value' => t('Save'),
  33. '#weight' => 5,
  34. );
  35. return $form;
  36. }
  37. /**
  38. * Validation callback.
  39. */
  40. function field_collection_item_form_validate($form, &$form_state) {
  41. entity_form_field_validate('field_collection_item', $form, $form_state);
  42. }
  43. /**
  44. * Submit builder. Extracts the form values and updates the entity.
  45. */
  46. function field_collection_item_form_submit_build_field_collection($form, $form_state) {
  47. entity_form_submit_build_entity('field_collection_item', $form_state['field_collection_item'], $form, $form_state);
  48. return $form_state['field_collection_item'];
  49. }
  50. /**
  51. * Submit callback that permanently saves the changes to the entity.
  52. */
  53. function field_collection_item_form_submit($form, &$form_state) {
  54. $field_collection_item = field_collection_item_form_submit_build_field_collection($form, $form_state);
  55. $field_collection_item->save();
  56. drupal_set_message(t('The changes have been saved.'));
  57. $form_state['redirect'] = $field_collection_item->path();
  58. }
  59. /**
  60. * Form for deleting a field collection item.
  61. */
  62. function field_collection_item_delete_confirm($form, &$form_state, $field_collection_item) {
  63. $form_state += array('field_collection_item' => $field_collection_item);
  64. return confirm_form($form,
  65. t('Are you sure you want to delete %label?', array('%label' => $field_collection_item->label())),
  66. $field_collection_item->path(),
  67. t('This action cannot be undone.'),
  68. t('Delete'),
  69. t('Cancel')
  70. );
  71. }
  72. /**
  73. * Submit callback for deleting a field collection item.
  74. */
  75. function field_collection_item_delete_confirm_submit($form, &$form_state) {
  76. $field_collection_item = $form_state['field_collection_item'];
  77. $field_collection_item->deleteRevision();
  78. drupal_set_message(t('%label has been deleted.', array('%label' => drupal_ucfirst($field_collection_item->label()))));
  79. $form_state['redirect'] = '<front>';
  80. }
  81. /**
  82. * Add a new field collection item.
  83. *
  84. * @todo: Support optionally passing in the revision_id and langcode parameters.
  85. */
  86. function field_collection_item_add($field_name, $entity_type, $entity_id, $revision_id = NULL, $langcode = NULL) {
  87. $info = entity_get_info();
  88. if (!isset($info[$entity_type])) {
  89. return MENU_NOT_FOUND;
  90. }
  91. $result = entity_load($entity_type, array($entity_id));
  92. $entity = reset($result);
  93. if (!$entity) {
  94. return MENU_NOT_FOUND;
  95. }
  96. // Ensure the given entity is of a bundle that has an instance of the field.
  97. list($id, $rev_id, $bundle) = entity_extract_ids($entity_type, $entity);
  98. $instance = field_info_instance($entity_type, $field_name, $bundle);
  99. if (!$instance) {
  100. return MENU_NOT_FOUND;
  101. }
  102. // Check field cardinality.
  103. $field = field_info_field($field_name);
  104. $langcode = !empty($field['translatable']) ? entity_language($entity_type, $entity) : LANGUAGE_NONE;
  105. if (!($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || !isset($entity->{$field_name}[$langcode]) || count($entity->{$field_name}[$langcode]) < $field['cardinality'])) {
  106. drupal_set_message(t('Too many items.'), 'error');
  107. return '';
  108. }
  109. $field_collection_item = entity_create('field_collection_item', array('field_name' => $field_name));
  110. // Do not link the field collection item with the host entity at this point,
  111. // as during the form-workflow we have multiple field collection item entity
  112. // instances, which we don't want link all with the host.
  113. // That way the link is going to be created when the item is saved.
  114. $field_collection_item->setHostEntity($entity_type, $entity, $langcode, FALSE);
  115. $label = $field_collection_item->translatedInstanceLabel();
  116. $title = ($field['cardinality'] == 1) ? $label : t('Add new !instance_label', array('!instance_label' => $label));
  117. drupal_set_title($title);
  118. // Make sure the current user has access to create a field collection item.
  119. if (!entity_access('create', 'field_collection_item', $field_collection_item)) {
  120. return MENU_ACCESS_DENIED;
  121. }
  122. return drupal_get_form('field_collection_item_form', $field_collection_item);
  123. }