Commit c34f0ed5 authored by Ivan Duarte's avatar Ivan Duarte Committed by Derek Wright
Browse files

Issue #2813405: Add a field to view and edit content groups (patch #91)

parent cb58c30b
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
entity_field_label:
  user: 'Group Memberships'
 No newline at end of file
+52 −0
Original line number Diff line number Diff line
.js .field--widget-group-selector-widget .dropbutton-wrapper {
  display: inline-flex;
  padding-right: 0em;
  margin-right: 0em;
  /* Override 600px breakpoint from core. */
  width: auto;
}

.js .field--widget-group-selector-widget .dropbutton-widget {
  position: relative;
}

.js .field--widget-group-selector-widget .field-multiple-table {
  margin-bottom: 10px;
}

.js .field--widget-group-selector-widget td {
  padding: 10px 0px 10px 0px;
}

.js .field--widget-group-selector-widget .field-multiple-drag {
  vertical-align: top;
}
.js .field--widget-group-selector-widget .draggable .tabledrag-handle {
  padding-right: 0;
  margin-left: 0;
}
.js .field--widget-group-selector-widget .tabledrag-handle .handle {
  margin-left: 0;
  margin-right: 0;
  padding-right: 0.2em;
}
.js .gcontent-type-top {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.js .gcontent-type-title {
  flex-basis: 25%;
  min-width: 80px;

  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.js .field--widget-group-selector-widget .delta-order {
  padding-right: 10px;
  text-align: right;
}
.js .field--widget-group-selector-widget .dropbutton-action .ajax-progress {
  left: -115px;
}
+8 −0
Original line number Diff line number Diff line
name: 'Group Content Field'
description: 'Provide a new field which allows you to add group content from entity form'
package: 'Group'
type: 'module'
version: 1.0
core: '8.x'
dependencies:
  - 'group:group'
+12 −0
Original line number Diff line number Diff line
gcontent_field.admin:
  dependencies:
      - core/jquery
      - core/drupal
      - core/drupalSettings
      - core/jquery.once
      - core/jquery.form
      - core/drupal.ajax
      - core/drupal.dropbutton
  css:
    theme:
      css/gcontent-field.admin.css: {}
 No newline at end of file
+77 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Allows to add group content from entity form.
 */

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;

/**
 * Implements hook_theme().
 */
function gcontent_field_theme() {
  return [
    'gc_field_dropbutton_wrapper' => [
      'variables' => ['children' => NULL],
    ],
  ];
}

/**
 * Implements hook_entity_base_field_info().
 */
function gcontent_field_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_types = gcontent_field_get_entity_types()) {
    // Adding field to entity types.
    if (array_key_exists($entity_type->id(), $entity_types)) {
      $entity_field_label = \Drupal::config('gcontent_field.settings')->get('entity_field_label');
      $field_label = $entity_field_label[$entity_type->id()] ?? t('Groups');
      $fields['group_content'] = BaseFieldDefinition::create('entity_reference')
        ->setName('group_content')
        ->setTargetEntityTypeId($entity_type->id())
        ->setSetting('target_type', 'group_content')
        ->setLabel($field_label)
        ->setTranslatable(FALSE)
        ->setComputed(TRUE)
        ->setCustomStorage(TRUE)
        ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
        ->setClass('\Drupal\gcontent_field\Field\GcontentFieldItemList')
        ->setDisplayConfigurable('form', TRUE)
        ->setDisplayOptions('form', [
          'type' => 'hidden',
          'weight' => 50,
        ])
        ->setDisplayConfigurable('view', TRUE)
        ->setDisplayOptions('view', [
          'label' => 'hidden',
          'type' => 'hidden',
          'weight' => 50,
        ]);
    }
  }
  return $fields;
}

/**
 * Get entities where the field should be added.
 */
function gcontent_field_get_entity_types() {
  $entity_types = [];
  $plugin_manager = \Drupal::service('plugin.manager.group_content_enabler');
  foreach ($plugin_manager->getDefinitions() as $plugin_id => $plugin_definition) {
    // If entity already added to list just continue with next plugin.
    if (in_array($plugin_definition['entity_type_id'], $entity_types)) {
      continue;
    }
    $entity_type_definition = \Drupal::entityTypeManager()->getDefinition($plugin_definition['entity_type_id']);
    // Check if fields can be attached to the entity type.
    if ($entity_type_definition->get('field_ui_base_route')) {
      $entity_types[$plugin_definition['entity_type_id']] = (string) $entity_type_definition->getLabel();
    }
  }
  return $entity_types;
}
Loading