PHP.php in Custom Formatters 8.3
File
src/Plugin/CustomFormatters/FormatterType/PHP.php
View source
<?php
namespace Drupal\custom_formatters\Plugin\CustomFormatters\FormatterType;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\custom_formatters\FormatterTypeBase;
class PHP extends FormatterTypeBase {
public function settingsForm(array &$form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form['data']['#description'] = $this
->t('Enter the PHP code that will be evaluated. You should NOT include %php tags.<br /><br /><strong>Available parameters:</strong><dl><dt><em><a href=":field_item_list_inerface" target="_blank">FieldItemListInterface</a></em> $items</dt><dd>The field values to be rendered.</dd><dt><em>string</em> $langcode</dt><dd>The language that should be used to render the field.</dd></dt></dl>', [
'%php' => '<?php ?>',
':field_item_list_inerface' => 'https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21FieldItemListInterface.php/interface/FieldItemListInterface',
]);
return $form;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
ob_start();
$output = eval($this->entity
->get('data'));
$output = !empty($output) ? $output : ob_get_contents();
ob_end_clean();
return empty($output) ? FALSE : $output;
}
}
Classes
Name |
Description |
PHP |
Plugin implementation of the PHP Formatter type. |