Skip to content
Snippets Groups Projects
Commit c754c4dc authored by Owen Bush's avatar Owen Bush
Browse files

Initial set up for new reference field for inheritance

parent f45c8b72
No related branches found
No related tags found
No related merge requests found
<?php
namespace Drupal\field_inheritance\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceEntityFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'entity reference rendered entity' formatter.
*
* @FieldFormatter(
* id = "field_inheritance_default",
* label = @Translation("Inherited Field"),
* description = @Translation("Display the inherited field."),
* field_types = {
* "field_inheritance"
* }
* )
*/
class FieldInheritanceDefaultFormatter extends EntityReferenceEntityFormatter {
/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'formatter' => NULL,
] + parent::defaultSettings();
}
/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements['formatter'] = [
'#type' => 'select',
'#options' => $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type')),
'#title' => t('View mode'),
'#default_value' => $this->getSetting('view_mode'),
'#required' => TRUE,
];
return $elements;
}
/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = [];
$view_modes = $this->entityDisplayRepository->getViewModeOptions($this->getFieldSetting('target_type'));
$formatter = $this->getSetting('formatter');
$summary[] = t('Rendered as @formatter', ['@formatter' => 'blah']);
return $summary;
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$view_mode = $this->getSetting('view_mode');
$elements = [];
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
// Due to render caching and delayed calls, the viewElements() method
// will be called later in the rendering process through a '#pre_render'
// callback, so we need to generate a counter that takes into account
// all the relevant information about this field and the referenced
// entity that is being rendered.
$recursive_render_id = $items->getFieldDefinition()->getTargetEntityTypeId()
. $items->getFieldDefinition()->getTargetBundle()
. $items->getName()
// We include the referencing entity, so we can render default images
// without hitting recursive protections.
. $items->getEntity()->id()
. $entity->getEntityTypeId()
. $entity->id();
if (isset(static::$recursiveRenderDepth[$recursive_render_id])) {
static::$recursiveRenderDepth[$recursive_render_id]++;
}
else {
static::$recursiveRenderDepth[$recursive_render_id] = 1;
}
// Protect ourselves from recursive rendering.
if (static::$recursiveRenderDepth[$recursive_render_id] > static::RECURSIVE_RENDER_LIMIT) {
$this->loggerFactory->get('entity')->error('Recursive rendering detected when rendering entity %entity_type: %entity_id, using the %field_name field on the %bundle_name bundle. Aborting rendering.', [
'%entity_type' => $entity->getEntityTypeId(),
'%entity_id' => $entity->id(),
'%field_name' => $items->getName(),
'%bundle_name' => $items->getFieldDefinition()->getTargetBundle(),
]);
return $elements;
}
$view_builder = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId());
$elements[$delta] = $view_builder->view($entity, $view_mode, $entity->language()->getId());
// Add a resource attribute to set the mapping property's value to the
// entity's url. Since we don't know what the markup of the entity will
// be, we shouldn't rely on it for structured data such as RDFa.
if (!empty($items[$delta]->_attributes) && !$entity->isNew() && $entity->hasLinkTemplate('canonical')) {
$items[$delta]->_attributes += ['resource' => $entity->toUrl()->toString()];
}
}
return $elements;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
// This formatter is only available for entity types that have a view
// builder.
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
return \Drupal::entityManager()->getDefinition($target_type)->hasViewBuilderClass();
}
}
<?php
namespace Drupal\field_inheritance\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'field_inheritance' field type.
*
* @FieldType (
* id = "field_inheritance",
* label = @Translation("Field Inheritance"),
* description = @Translation("An entity field containing a field inheritance reference."),
* category = @Translation("Reference"),
* default_widget = "field_inheritance_autocomplete",
* default_formatter = "field_inheritance_default",
* list_class = "\Drupal\Core\Field\EntityReferenceFieldItemList",
* )
*/
class FieldInheritanceItem extends EntityReferenceItem {
/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = parent::schema($field_definition);
$schema['columns']['field_name'] = [
'type' => 'varchar',
'length' => 255,
];
return $schema;
}
/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties = parent::propertyDefinitions($field_definition);
// Add our properties.
$properties['field_name'] = DataDefinition::create('string')
->setLabel(t('Field Name'))
->setDescription(t('The name of the field to inherit'));
return $properties;
}
}
<?php
namespace Drupal\field_inheritance\Plugin\Field\FieldWidget;
use Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget;
/**
* Plugin implementation of the 'field_inheritance_autocomplete' widget.
*
* @FieldWidget(
* id = "field_inheritance_autocomplete",
* label = @Translation("Autocomplete"),
* description = @Translation("An autocomplete text field."),
* field_types = {
* "field_inheritance"
* }
* )
*/
class FieldInheritanceAutocompleteWidget extends EntityReferenceAutocompleteWidget {
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment