ExtraFieldPlusDisplayFormattedBase.php in Extra Field Settings Provider 8.2
File
src/Plugin/ExtraFieldPlusDisplayFormattedBase.php
View source
<?php
namespace Drupal\extra_field_plus\Plugin;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\Element;
use Drupal\extra_field\Plugin\ExtraFieldDisplayFormattedInterface;
abstract class ExtraFieldPlusDisplayFormattedBase extends ExtraFieldPlusDisplayBase implements ExtraFieldPlusDisplayInterface, ExtraFieldDisplayFormattedInterface {
protected $isEmpty = FALSE;
protected $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED;
public function view(ContentEntityInterface $entity) {
$elements = $this
->viewElements($entity);
if (!empty($elements) && !$this
->isEmpty()) {
$build = [
'#theme' => 'field',
'#title' => $this
->getLabel(),
'#label_display' => $this
->getLabelDisplay(),
'#view_mode' => '_custom',
'#language' => $this
->getLangcode(),
'#field_name' => $this
->getFieldName(),
'#field_type' => $this
->getFieldType(),
'#field_translatable' => $this
->isTranslatable(),
'#entity_type' => $entity
->getEntityTypeId(),
'#bundle' => $entity
->bundle(),
'#object' => $entity,
'#formatter' => $this
->getPluginId(),
];
if ($children = Element::children($elements, TRUE)) {
$build['#is_multiple'] = TRUE;
$build['#children'] = '';
foreach ($children as $key) {
$build['#items'][$key] = new \stdClass();
$build[$key] = $elements[$key];
}
}
else {
$build['#is_multiple'] = FALSE;
$build['#items'][] = new \stdClass();
$build[] = $elements;
}
}
else {
$build = $elements;
}
return $build;
}
public function getLabel() {
return '';
}
public function getLabelDisplay() {
return 'hidden';
}
public function getFieldType() {
return 'extra_field';
}
public function getFieldName() {
return 'extra_field_' . $this->pluginId;
}
public function isEmpty() {
return $this->isEmpty;
}
public function setLangcode($langcode) {
$this->langcode = $langcode;
}
public function getLangcode() {
return $this->langcode;
}
public function isTranslatable() {
return FALSE;
}
}