Skip to content

Instantly share code, notes, and snippets.

@seutje
Last active August 31, 2016 10:16
Show Gist options
  • Save seutje/26d4516874676864094c28e8080bd21c to your computer and use it in GitHub Desktop.
Save seutje/26d4516874676864094c28e8080bd21c to your computer and use it in GitHub Desktop.
Clone a field!
<?php
/**
* @file
* DS field clone.
*/
namespace Drupal\mymodule\Plugin\DsField;
use Drupal\ds\Plugin\DsField\DsFieldBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin that renders a clone of a field.
*
* @DsField(
* id = "mymodule_field_clone",
* title = @Translation("DS Field: Field clone"),
* entity_type = "node",
* provider = "mymodule",
* ui_limit = { "*|*"}
* )
*/
class FieldClone extends DsFieldBase {
/**
* Entity manager.
*
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity.manager')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$room = $this->entity();
$config = $this->getConfiguration();
$viewBuilder = $this->entityTypeManager->getViewBuilder('node');
$field = $room->field_name;
$output = $viewBuilder->viewField($field, $config['view_mode']);
return $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment