Skip to content
Closed
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
80 changes: 80 additions & 0 deletions src/Plugin/UiPatterns/Source/ExtraFieldSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Drupal\ui_patterns\Plugin\UiPatterns\Source;

use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ui_patterns\Plugin\PatternSourceBase;
use Drupal\ui_patterns\UiPatternsManager;
use Drupal\Core\TypedData\TypedDataManager;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Defines Fields API pattern source plugin.
*
* @UiPatternsSource(
* id = "extra_fields",
* label = @Translation("Extra fields"),
* provider = "field",
* tags = {
* "entity_display"
* }
* )
*/
class ExtraFieldSource extends PatternSourceBase implements ContainerFactoryPluginInterface {

/**
* Entity field manager service.
*
* @var \Drupal\Core\Entity\EntityFieldManager
*/
protected $entityFieldManager;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, UiPatternsManager $ui_patterns_manager, TypedDataManager $typed_data_manager, EntityFieldManager $entity_field_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityFieldManager = $entity_field_manager;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.ui_patterns'),
$container->get('typed_data_manager'),

Choose a reason for hiding this comment

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

Why add UiPatternsManager and TypedDataManager to the constructor, if they aren't being stored or used anywhere?

$container->get('entity_field.manager')
);
}

/**
* {@inheritdoc}
*/
public function getSourceFields() {
$sources = [];
$entity_type_id = $this->getContextProperty('entity_type');
$bundle = $this->getContextProperty('entity_bundle');
$extra_fields = $this->entityFieldManager->getExtraFields($entity_type_id, $bundle);

if (!isset($extra_fields['display'])) {
return $sources;
}

foreach ($extra_fields['display'] as $extra_field_name => $field) {
if (!$this->getContextProperty('limit')) {
$sources[] = $this->getSourceField($extra_field_name, $field['label']);
}
elseif (in_array($extra_field_name, $this->getContextProperty('limit'))) {
Copy link

Choose a reason for hiding this comment

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

These conditions could be merged with a "||"

if (!$this->getContextProperty('limit') || in_array($extra_field_name, $this->getContextProperty('limit'))) {

$sources[] = $this->getSourceField($extra_field_name, $field['label']);
}
}

return $sources;
}

}