Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions og_ui/og_ui.module
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,10 @@ function og_ui_entity_type_save(EntityInterface $entity) {

// Change the field target type and bundle.
if ($field_storage = FieldStorageConfig::loadByName($entity_type_id, OgGroupAudienceHelper::DEFAULT_FIELD)) {
$target_type = $field_storage->getSetting('target_type');
if ($entity->og_target_type !== $target_type) {
// @todo It's probably not possible to change the field storage after the
// field has data. We should disable this option in the UI.
$field_storage->setSetting('target_type', $entity->og_target_type);
$field_storage->save();
}
// @todo It's probably not possible to change the field storage after the
// field has data. We should disable this option in the UI.
$field_storage->setSetting('target_type', $entity->og_target_type);
$field_storage->save();
}
if ($field = FieldConfig::loadByName($entity_type_id, $bundle, OgGroupAudienceHelper::DEFAULT_FIELD)) {
$handler_settings = $field->getSetting('handler_settings');
Expand Down
108 changes: 107 additions & 1 deletion src/OgGroupAudienceHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldFilteredMarkup;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\og\Plugin\Field\FieldWidget\OgComplex;
use Drupal\Component\Utility\Html;

/**
* OG audience field helper methods.
Expand Down Expand Up @@ -128,6 +134,106 @@ public static function getMatchingField(ContentEntityInterface $entity, $group_t
return NULL;
}

/**
* Get list of available widgets.
*
* @return array
* List of available entity reference widgets.
*/
public static function getAvailableWidgets() {
$widget_manager = \Drupal::getContainer()->get('plugin.manager.field.widget');
$definitions = $widget_manager->getDefinitions();

$widgets = [];
foreach ($definitions as $id => $definition) {

if (!in_array('entity_reference', $definition['field_types'])) {
continue;
}

$widgets[] = $id;
}

return $widgets;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for splitting this off as static methods in OgGroupAudienceHelper? Are you planning on reusing them somewhere else?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - we would like to get all the available widget so the user could pick them through the UI. No?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Way to go! That's the spirit!


/**
* Set the field mode widget.
*
* @param $entity_id
* The entity id.
* @param $bundle
* The bundle.
* @param $field_name
* The field name.
* @param array $modes
* The field modes. Available keys: default, admin.
*
* @return int
* Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
*/
public static function setWidgets($entity_id, $bundle, $field_name, array $modes) {
$field = FieldConfig::loadByName($entity_id, $bundle, $field_name);
$handler = $field->getSetting('handler_settings');
$handler['widgets'] = $modes;
$field->setSetting('handler_settings', $handler);
return $field->save();
}

/**
* get the field mode widget.
*
* @param $entity_id
* The entity id.
* @param $bundle
* The bundle.
* @param $field_name
* The field name.
* @param null $mode
* The field mode - admin or default.
*
* @return array.
* The field modes.
*/
public static function getWidgets($entity_id, $bundle, $field_name, $mode = NULL) {
$field = FieldConfig::loadByName($entity_id, $bundle, $field_name);
$handler = $field->getSetting('handler_settings');
return $mode ? $handler['widgets'][$mode] : $handler['handler_settings']['widgets'];
}

/**
* @param FieldDefinitionInterface $field
* The field definition.
* @param $widget_id
* An entity reference widget plugin id i.e: options_select, options_buttons.
* @param string $field_name
* The field name. Default to self::DEFAULT_FIELD.
* @param array $configuration
* Configuration which will be passed to the widget instance.
*
* @return WidgetBase The form API widget element.
* The form API widget element.
*/
public static function renderWidget(FieldDefinitionInterface $field, $widget_id, $field_name = self::DEFAULT_FIELD, array $configuration = []) {
$config = FieldConfig::load($field->getTargetEntityTypeId() . '.' . $field->getTargetBundle() . '.' . $field_name);

$default_configuration = $configuration + [
'type' => 'og_complex',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => 60,
'placeholder' => '',
],
'third_party_settings' => [],
'field_definition' => $config,
];

return \Drupal::getContainer()
->get('plugin.manager.field.widget')
->createInstance($widget_id, $default_configuration);

}

/**
* Return all the group audience fields of a certain bundle.
*
Expand Down
1 change: 1 addition & 0 deletions src/Plugin/Field/FieldType/OgMembershipReferenceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Session\AccountInterface;

/**
* Class OgMembershipReferenceItem.
Expand Down
30 changes: 30 additions & 0 deletions src/Plugin/Field/FieldType/OgStandardReferenceItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\og\Og;

/**
* Class OgStandardReferenceItem.
Expand Down Expand Up @@ -55,4 +57,32 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* Overrides parent::getSettableOptions().
*
* Return the list of allowed groups to reference the content to.
*/
public function getSettableOptions(AccountInterface $account = NULL) {
$field_definition = $this->getFieldDefinition();
$field_mode = !empty($field_definition->otherGroup) ? 'admin' : 'default';

if (!$options = Og::getSelectionHandler($this->getFieldDefinition(), ['handler_settings' => ['field_mode' => $field_mode]])->getReferenceableEntities()) {
return array();
}

// Rebuild the array by changing the bundle key into the bundle label.
$target_type = $field_definition->getSetting('target_type');
$bundles = \Drupal::entityManager()->getBundleInfo($target_type);

$return = array();
foreach ($options as $bundle => $entity_ids) {
// The label does not need sanitizing since it is used as an optgroup
// which is only supported by select elements and auto-escaped.
$bundle_label = (string) $bundles[$bundle]['label'];
$return[$bundle_label] = $entity_ids;
}

return count($return) == 1 ? reset($return) : $return;
}

}
Loading