diff --git a/field-collection-item.tpl.php b/field-collection-item.tpl.php index 8d345cb..a3a5453 100644 --- a/field-collection-item.tpl.php +++ b/field-collection-item.tpl.php @@ -26,7 +26,7 @@ * @see template_preprocess() * @see template_preprocess_entity() * @see template_process() - */ + */ ?>
>
> diff --git a/field_collection.module b/field_collection.module index 80e4588..4c3e32a 100644 --- a/field_collection.module +++ b/field_collection.module @@ -60,6 +60,33 @@ function field_collection_entity_info() { ); } + + /* RW: patch for views view entity op links, http://drupal.org/node/1309332. + As long as I'm pulling op links out, maybe make them follow this mold. + + // use field_read_fields() as field_info_fields() requires built entity info + // to work. + foreach (field_read_fields(array('type' => 'field_collection')) as $field_name => $field) { + $path = field_collection_field_get_path($field); + $count = count(explode('/', $path)); + $return['field_collection_item']['bundles'][$field_name] = array( + 'label' => t('Field collection @field', array('@field' => $field_name)), + 'admin' => array( + 'path' => 'admin/structure/field-collections/%field_collection_field_name', + 'real path' => 'admin/structure/field-collections/' . strtr($field_name, array('_' => '-')), + 'bundle argument' => 3, + 'access arguments' => array('administer field collections'), + ), + 'crud' => array( + 'add' => array('path' => $path . '/add'), + 'view' => array('path' => $path . '/%', 'entity_id' => $count), + 'edit' => array('path' => $path . '/%/edit', 'entity_id' => $count), + 'delete' => array('path' => $path . '/%/delete', 'entity_id' => $count) + ), + ); + } + */ + return $return; } @@ -560,25 +587,67 @@ function field_collection_item_access($op, FieldCollectionItemEntity $item = NUL if (!isset($item)) { return FALSE; } - $op = $op == 'view' ? 'view' : 'edit'; - // Access is determined by the entity and field containing the reference. + $field = field_info_field($item->field_name); - $entity_access = entity_access($op == 'view' ? 'view' : 'update', $item->hostEntityType(), $item->hostEntity(), $account); - return $entity_access && field_access($op, $field, $item->hostEntityType(), $item->hostEntity(), $account); + + // Entity access takes 'view', 'update', 'create' or 'delete' operations. + if ($op == 'edit') { + $op = 'update'; + } + // Field access takes only 'view' or 'edit' operations. + $field_op = $op == 'view' ? 'view' : 'edit'; + + // Access is determined by the entity and field containing the reference. + return entity_access($op, $item->hostEntityType(), $item->hostEntity(), $account) && field_access($field_op, $field, $item->hostEntityType(), $item->hostEntity(), $account); } /** * Implements hook_theme(). */ -function field_collection_theme() { +function field_collection_theme($existing, $type, $theme, $path) { + + $path = drupal_get_path('module', 'field_collection'); + return array( + 'field_collection_item' => array( 'render element' => 'elements', 'template' => 'field-collection-item', ), + 'field_collection_view' => array( 'render element' => 'element', ), + + /* RW: mine + + 'field_collection' => array( + 'path' => $path . '/includes/theme', + 'template' => 'field-collection', + // Should be fc element + 'render element' => 'element', + ), + + 'field_collection_item' => array( + 'path' => $path . '/includes/theme', + 'template' => 'field-collection-item', + // Should be fc item element + 'render element' => 'element', + ), + + 'field_collection_item_link' => array( + 'path' => $path . '/includes/theme', + 'template' => 'field-collection-item-link', + // Should be fc item element ? + 'render element' => 'element', + ), + */ + + 'field_collection_op_links' => array( + 'path' => $path . '/includes/theme', + 'template' => 'field-collection-op-links', + 'render element' => 'op_links', + ), ); } @@ -778,6 +847,7 @@ function field_collection_field_formatter_info() { ); } + /** * Implements hook_field_formatter_settings_form(). */ @@ -809,7 +879,7 @@ function field_collection_field_formatter_settings_form($field, $instance, $view '#type' => 'checkbox', '#title' => t('Show the field description beside the add link.'), '#default_value' => $settings['description'], - '#description' => t('If enabled and the add link is shown, the field description is shown in front of the add link.'), + '#description' => t('If enabled and the add link is shown, the field description is shown next to the add link.'), ); } @@ -820,6 +890,7 @@ function field_collection_field_formatter_settings_form($field, $instance, $view $options = array(); foreach ($entity_type['view modes'] as $mode => $info) { $options[$mode] = $info['label']; + } $elements['view_mode'] = array( @@ -867,17 +938,20 @@ function field_collection_field_formatter_settings_summary($field, $instance, $v */ function field_collection_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) { $element = array(); + $settings = $display['settings']; switch ($display['type']) { case 'field_collection_list': foreach ($items as $delta => $item) { + if ($field_collection = field_collection_field_get_entity($item)) { + $output = l($field_collection->label(), $field_collection->path()); $links = array(); foreach (array('edit', 'delete') as $op) { - if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) { + if ($settings[$op] && field_collection_item_access($op, $field_collection)) { $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]); $links[] = l($title, $field_collection->path() . '/' . $op, array('query' => drupal_get_destination())); } @@ -895,36 +969,61 @@ function field_collection_field_formatter_view($entity_type, $entity, $field, $i $element['#attached']['css'][] = drupal_get_path('module', 'field_collection') . '/field_collection.theme.css'; $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full'; + foreach ($items as $delta => $item) { - if ($field_collection = field_collection_field_get_entity($item)) { - $element[$delta]['entity'] = $field_collection->view($view_mode); - $element[$delta]['#theme_wrappers'] = array('field_collection_view'); - $element[$delta]['#attributes']['class'][] = 'field-collection-view'; - $element[$delta]['#attributes']['class'][] = 'clearfix'; - $element[$delta]['#attributes']['class'][] = drupal_clean_css_identifier('view-mode-' . $view_mode); - $links = array( - '#theme' => 'links__field_collection_view', - ); - $links['#attributes']['class'][] = 'field-collection-view-links'; - foreach (array('edit', 'delete') as $op) { - if ($settings[$op] && field_collection_item_access($op == 'edit' ? 'update' : $op, $field_collection)) { - $links['#links'][$op] = array( - 'title' => entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]), - 'href' => $field_collection->path() . '/' . $op, - 'query' => drupal_get_destination(), - ); - } + $field_collection_item = field_collection_field_get_entity($item); + + $element[$delta]['entity'] = $field_collection_item->view($view_mode); + + // RW removing this to test. + //$element[$delta]['#theme_wrappers'] = array('field_collection_view'); + + $element[$delta]['#attributes']['class'][] = 'field-collection-view'; + $element[$delta]['#attributes']['class'][] = 'clearfix'; + $element[$delta]['#attributes']['class'][] = drupal_clean_css_identifier('view-mode-' . $view_mode); + + + + /* RW Removing EDIT links to test new formatter. + + $links = array( + '#theme' => 'links__field_collection_view', + ); + $links['#attributes']['class'][] = 'field-collection-view-links'; + foreach (array('edit', 'delete') as $op) { + + + if ($settings[$op] && field_collection_item_access($op, $field_collection)) { + $links['#links'][$op] = array( + 'title' => entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]), + 'href' => $field_collection->path() . '/' . $op, + 'query' => drupal_get_destination(), + ); } - $element[$delta]['links'] = $links; + } + + $element[$delta]['links'] = $links; + + */ + + // Add edit and delete operation links if available. + $op_links = field_collection_field_formatter_op_links($entity_type, $entity, $field, $instance, $langcode, $items, $settings, $ops = array('edit', 'delete'), $item, $field_collection_item); + !empty($op_links) ? $element[$delta]['op_links'] = $op_links : ''; + } - field_collection_field_formatter_links($element, $entity_type, $entity, $field, $instance, $langcode, $items, $display); + + // Add the add operation link if available. + $op_links = field_collection_field_formatter_op_links($entity_type, $entity, $field, $instance, $langcode, $items, $settings, $ops = array('add')); + !empty($op_links) ? $element['op_links'] = $op_links : ''; + break; case 'field_collection_fields': $view_mode = !empty($display['settings']['view_mode']) ? $display['settings']['view_mode'] : 'full'; + foreach ($items as $delta => $item) { if ($field_collection = field_collection_field_get_entity($item)) { $element[$delta]['entity'] = $field_collection->view($view_mode); @@ -938,35 +1037,48 @@ function field_collection_field_formatter_view($entity_type, $entity, $field, $i /** * Helper function to add links to a field collection field. - */ + * + + RW removing for now to test new formatter + function field_collection_field_formatter_links(&$element, $entity_type, $entity, $field, $instance, $langcode, $items, $display) { $settings = $display['settings']; + // Check if there is room to add more field collection items. if ($settings['add'] && ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || count($items) < $field['cardinality'])) { - // Check whether the current is allowed to create a new item. + + // Make a dummy field collection item to test against in case no field collection items have been created yet. $field_collection_item = entity_create('field_collection_item', array('field_name' => $field['field_name'])); $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE); + // Check whether the current user is allowed to create a new item. if (field_collection_item_access('create', $field_collection_item)) { + $path = field_collection_field_get_path($field); - list($id) = entity_extract_ids($entity_type, $entity); + + // Get parent entity id. + list($entity_id) = entity_extract_ids($entity_type, $entity); + $element['#suffix'] = ''; + + // RW Description Building, Boring. if (!empty($settings['description'])) { $element['#suffix'] .= '
' . field_filter_xss($instance['description']) . '
'; } + $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_add", $settings['add']); - $add_path = $path . '/add/' . $entity_type . '/' . $id; + $add_path = $path . '/add/' . $entity_type . '/' . $entity_id; $element['#suffix'] .= ''; + } } - // If there is no add link, add a special class to the last item. + if (empty($element['#suffix'])) { $index = count(element_children($element)) - 1; $element[$index]['#attributes']['class'][] = 'field-collection-view-final'; } - $element += array('#prefix' => '', '#suffix' => ''); $element['#prefix'] .= '
'; $element['#suffix'] .= '
'; @@ -974,6 +1086,59 @@ function field_collection_field_formatter_links(&$element, $entity_type, $entity return $element; } +*/ + + +/** + RW new ops link formatter. + Takes $ops as an array. + */ + +function field_collection_field_formatter_op_links($entity_type, $entity, $field, $instance, $langcode, $items, $settings, $ops, $item = NULL, $field_collection_item = NULL) { + + // If the $op is add, check if there is room for more field collection items. + $room_for_more = in_array('add', $ops) ? ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || count($items) < $field['cardinality']) : TRUE; + + if (in_array('add', $ops)) { + // Create a dummy field collection item to test against in case none have been added yet. + $field_collection_item = entity_create('field_collection_item', array('field_name' => $field['field_name'])); + $field_collection_item->setHostEntity($entity_type, $entity, LANGUAGE_NONE, FALSE); + } + + // Create the render element. + $op_links = array(); + + foreach ($ops as $op) { + + // Check if $op links should be added. + if (!empty($settings[$op]) && $room_for_more && field_collection_item_access($op, $field_collection_item)) { + + // Build link. + $title = entity_i18n_string("field:{$field['field_name']}:{$instance['bundle']}:setting_$op", $settings[$op]); + + if ($op == 'add') { + list($entity_id) = entity_extract_ids($entity_type, $entity); + $path = field_collection_field_get_path($field) . '/add/' . $entity_type . '/' . $entity_id; + } + else { + $path = $field_collection_item->path() . '/' . $op; + } + + $op_links['#links'][] = l($title, $path, array('query' => drupal_get_destination())); + + // Add description. + $op_links['#description'] = $op == 'add' && $settings['description'] ? field_filter_xss($instance['description']) : FALSE; + + } + + } + + if (!empty($op_links)) { + $op_links['#theme'] = 'field_collection_op_links'; + return $op_links; + } +} + /** * Themes field collection items printed using the field_collection_view formatter. */