ForwardFormFormatter.php in Forward 4.x
File
src/Plugin/Field/FieldFormatter/ForwardFormFormatter.php
View source
<?php
namespace Drupal\forward\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\forward\Services\ForwardFormBuilderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ForwardFormFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $formBuilder;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ForwardFormBuilderInterface $form_builder) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->formBuilder = $form_builder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
->get('forward.form_builder'));
}
public static function isApplicable(FieldDefinitionInterface $field_definition) {
return \Drupal::currentUser()
->hasPermission('access forward');
}
public function prepareView(array $entities_items) {
foreach ($entities_items as $item) {
if ($item
->isEmpty()) {
$display = TRUE;
$item
->appendItem($display);
}
}
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($items as $delta => $item) {
$display = $item
->get('display')
->getValue();
if (!empty($display)) {
$entity = $item
->getEntity();
$elements[$delta] = $this->formBuilder
->buildInlineForm($entity);
}
}
return $elements;
}
}