fieldry-1.0.x-dev/src/FieldDisplayInfo.php
src/FieldDisplayInfo.php
<?php
namespace Drupal\fieldry;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Field\FormatterInterface;
/**
* Helper class to fetch field display settings from display configurations.
*/
class FieldDisplayInfo {
const DEFAULT_VIEW_MODE = EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE;
/**
* Create a new instance of the FieldDisplayInfo utility helper.
*
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $displayRepo
* Repository for holding all the currently defined entity display view
* modes currently available for all the different entity types.
*/
public function __construct(protected EntityDisplayRepositoryInterface $displayRepo) {
}
/**
* Retrieve an entity display from the entity type, bundle and view mode.
*
* @param string $entity_type
* The entity type ID.
* @param string $bundle
* The entity bundle.
* @param string $view_mode
* The display view mode to fetch the display configurations for.
*
* @return \Drupal\Core\Entity\Display\EntityViewDisplayInterface|null
* A loaded entity display view if a matching one is available, or NULL
* if a matching entity display cannot be located.
*/
protected function getEntityDisplay(string $entity_type, string $bundle, string $view_mode): ?EntityViewDisplayInterface {
$displayModes = $this->displayRepo->getViewModes($entity_type);
$viewMode = empty($displayModes[$view_mode]['status'])
? self::DEFAULT_VIEW_MODE
: $view_mode;
return $this->displayRepo->getViewDisplay($entity_type, $bundle, $viewMode);
}
/**
* Find the field formatter for an entity display based on renderable array.
*
* @param array $element
* A renderable array including the entity type, bundle, view mode and field
* to retrieve the field formatter for.
*
* @return \Drupal\Core\Field\FormatterInterface|null
* Return a field formatter instance for the formatter matchining the
* constraints pointed to by the $element array. Returns NULL if failed to
* load a field formatter matching the field and display conditions.
*/
public function getFromRenderable(array $element): ?FormatterInterface {
if (isset($element['#entity_type']) && isset($element['#bundle'])) {
$entityType = $element['#entity_type'];
$bundle = $element['#bundle'];
$viewMode = $element['#view_mode'] ?? self::DEFAULT_VIEW_MODE;
if ($display = $this->getEntityDisplay($entityType, $bundle, $viewMode)) {
/** @var \Drupal\Core\Field\FormatterInterface|null $formatter */
$formatter = $display->getRenderer($element['#field_name']);
return $formatter;
}
}
return NULL;
}
/**
* Fetch fieldry entity display settings for a renderable field or property.
*
* @param array $element
* A renderable array including the entity type, bundle, view mode and field
* to retrieve the field formatter for.
*
* @return array
* Fieldry third party settings stored for the field formatter or other
* entity display renderable. Will return an array of default settings even
* if the field did not have settings previously defined.
*/
public function getDisplaySettings(array $element): array {
$settings = [];
if (isset($element['#third_party_settings'])) {
$settings = $element['#third_party_settings']['fieldry'] ?? [];
}
else {
$formatter = $this->getFromRenderable($element);
if ($formatter instanceof ThirdPartySettingsInterface) {
$settings = $formatter->getThirdPartySettings('fieldry') ?? [];
}
}
return $settings + [
'wrapper' => 'default',
'config' => [],
];
}
}
