# Summary

Provides a table formatter for the Field Collection module

# Project URL

https://www.drupal.org/project/field_collection_table

# Where is the code?

https://github.com/Chiranjeeb2410/Port-Field-Collection-Table

# Estimated completion date

Unknown

# Dependencies

http://drupal.org/project/field_collection

# Who's doing the port?

https://www.drupal.org/u/chiranjeeb2410
https://www.drupal.org/u/bhanuprakashnani

# What help do they need?

Reviews and suggestions

# D8 roadmap

https://www.drupal.org/node/2614618

# Background and reference information

This module is use by ERPAL for Service Providers at https://www.drupal.org/project/erpal

Comments

Francewhoa created an issue. See original summary.

napche’s picture

Just in case anyone needs a start for this, this actually works pretty good.

namespace Drupal\your_module\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\field\Entity\FieldConfig;
/**
 * Plugin implementation of the 'field_collection_table_formatter' formatter.
 *
 * @FieldFormatter(
 *   id = "field_collection_table_formatter",
 *   label = @Translation("Table"),
 *   field_types = {
 *     "field_collection"
 *   }
 * )
 */
class FieldCollectionTableFormatter extends FormatterBase {
  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {

    /**
     * Get config of field to get order of fields.
     *
     * TODO : make default configurable through settings.
     */
    $field_collection_field = $this->fieldDefinition->getName();
    $key = 'core.entity_view_display.field_collection_item.'.$field_collection_field.'.default';
    $content = \Drupal::config($key)->get('content');

    /**
     * Loop all items and get field labels and data.
     */
    foreach ($items as $delta => $item) {
      if($field_collection_item = $item->getFieldCollectionItem()) {
        $row = [];
        foreach ($field_collection_item->getFieldDefinitions() as $fieldname => $field_definition) {
          if (isset($content[$fieldname]) && $field_definition instanceof FieldConfig) {
            $weight = $content[$fieldname]['weight'];
            if (!isset($header[$weight])) {
              $header[$weight] = $field_definition->getLabel();
              $content[$fieldname]['label'] = 'hidden';
              $formatters[$fieldname] = \Drupal::service('plugin.manager.field.formatter')->getInstance(array(
                'field_definition' => $field_definition,
                'view_mode' => 'default',
                'configuration' => $content[$fieldname],
              ));
            }
            $formatter = $formatters[$fieldname];
            $entities = $field_collection_item->{$fieldname};
            $formatter->prepareView(array($entities));
            $build = $formatter->view($field_collection_item->{$fieldname});

            $row[$weight] = render($build);
          }
        }
        ksort($row);
        $rows[] = $row;
      }
    }
    ksort($header);

    $table = [
      '#type' => 'table',
      '#header' => $header,
      '#rows' => $rows,
    ];

    return ['#markup' => render($table)];
  }
}
imclean’s picture

@Napche The field_collection_table widget would be useful. A similar display formatter can be found here: Reference Table Formatter

francewhoa’s picture

@Francewhoa :) Note to myself. Updated attribution of contribution.

kris77’s picture

@Napche Where should I enter your code?
Can you explain how to do it please?
Thank you

mmjvb’s picture

In <Your module>/src/Plugin/Field/FieldFormatter/FieldCollectionTableFormatter.php.

kris77’s picture

@mmjvb thank you so much for your help.

This only works in full view mode and not with the ability to edit, delete or add a field collection item.

I will try to create the same as a widget.

Thanks again.

bhanuprakashnani’s picture

Hey everyone, Actually me and @chiranjeeb2410 are working on the port and are almost done with it. Our progress can be tracked here - https://github.com/BhanuPrakashNani/Port-Field-Collection-Table. Please feel free to review and give your suggestions.

mmjvb’s picture

Issue summary: View changes
Status: Active » Needs work

Set to Needs work due to #8.
See https://www.drupal.org/node/251466 for getting things done.

chiranjeeb2410’s picture

Issue summary: View changes
chiranjeeb2410’s picture

Hello people,

Working on the port of this module currently. The port is almost complete and the field_collection_table widget functionality works perfectly.
Moreover, the field formatter for the same is also on the run and almost ready for completion save for a small portion which I'm currently working on as of now. The updated code can be found here: https://github.com/Chiranjeeb2410/Port-Field-Collection-Table. I've also pushed the updated code for the same in d.o repo: https://www.drupal.org/project/field_collection_table_d8.

Hope to complete the remaining portion real soon!