Skip to content

Instantly share code, notes, and snippets.

@davereid
Last active September 30, 2015 18:31
Show Gist options
  • Save davereid/05cb49a1268e97f52cf5 to your computer and use it in GitHub Desktop.
Save davereid/05cb49a1268e97f52cf5 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Contains \Drupal\entity_embed\EntityEmbedDisplay\EntityEmbedFieldDisplay.
*/
namespace Drupal\entity_embed\EntityEmbedDisplay;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Render an individual field on an entity.
*
* @EntityEmbedDisplay(
* id = "field",
* label = @Translation("Rendered field")
* )
*/
class EntityEmbedFieldDisplay extends EntityEmbedDisplayBase {
/**
* The widget or formatter plugin manager.
*
* @var \Drupal\Component\Plugin\PluginManagerBase
*/
protected $pluginManager;
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account = NULL) {
return FALSE;
if (!parent::access($account)) {
return FALSE;
}
// Cannot render an entity if it does not have a view controller.
return $this->canRenderEntity($this->getContextValue('entity'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'field' => '',
'settings' => array(),
);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$field_options = array();
$definitions = $this->getFieldDefinitions();
foreach ($definitions as $key => $definition) {
$field_options[$key] = $definition->getLabel();
}
//$options = $this->getPluginOptions($field_definition);
debug($form);
$form['field'] = array(
'#type' => 'select',
'#title' => t('Field'),
'#options' => $field_options,
'#default_value' => $this->getConfigurationValue('field'),
'#required' => TRUE,
'#ajax' => array(
'callback' => array($this, 'updateFieldSettingsForm'),
'wrapper' => 'data-entity-embed-field-settings-wrapper',
'effect' => 'fade',
),
);
$form['type'] = array(
'#prefix' => '<div id="data-entity-embed-field-formatter-wrapper">',
'#suffix' => '</div>',
'#type' => 'select',
);
$form['settings']['data-entity-embed-settings'] = array(
'#type' => 'container',
'#prefix' => '<div id="data-entity-embed-field-settings-wrapper">',
'#suffix' => '</div>',
);
return $form;
}
/**
* {@inheritdoc}
*/
public function build() {
$entity = $this->getContextValue('entity');
$field = $this->getConfigurationValue('field');
//$formatter = $this->getConfigurationValue('formatter');
//$settings = $this->getConfigurationValue('settings');
//if ($formatter == 'view-mode') {
//}
return $entity->{$field}->view();
}
/**
* Collects the definitions of fields whose display is configurable.
*
* @return \Drupal\Core\Field\FieldDefinitionInterface[]
* The array of field definitions
*/
protected function getFieldDefinitions() {
$entity = $this->getContextValue('entity');
return array_filter($this->entityManager()->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle()), function ($field_definition) {
return $field_definition->isDisplayConfigurable('view');
});
}
/**
* Returns the extra fields of the entity type and bundle used by this form.
*
* @return array
* An array of extra field info.
*
* @see \Drupal\Core\Entity\EntityManagerInterface::getExtraFields()
*/
protected function getExtraFields() {
$entity = $this->getContextValue('entity');
$extra_fields = $this->entityManager()->getExtraFields($entity->getEntityTypeId(), $entity->bundle());
return isset($extra_fields['display']) ? $extra_fields['display'] : array();
}
/**
* Returns an array of widget or formatter options for a field.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field definition.
*
* @return array
* An array of widget or formatter options.
*/
protected function getPluginOptions(FieldDefinitionInterface $field_definition) {
$options = $this->pluginManager->getOptions($field_definition->getType());
$applicable_options = array();
foreach ($options as $option => $label) {
$plugin_class = DefaultFactory::getPluginClass($option, $this->pluginManager->getDefinition($option));
if ($plugin_class::isApplicable($field_definition)) {
$applicable_options[$option] = $label;
}
}
return $applicable_options;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment