IngredientRecipeMLFormatter.php in Recipe 8.2
File
src/Plugin/Field/FieldFormatter/IngredientRecipeMLFormatter.php
View source
<?php
namespace Drupal\recipe\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\ingredient\Plugin\Field\FieldFormatter\IngredientFormatter;
class IngredientRecipeMLFormatter extends IngredientFormatter {
public static function defaultSettings() {
$settings = parent::defaultSettings();
unset($settings['link']);
return $settings;
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$element = parent::settingsForm($form, $form_state);
unset($element['link']);
return $element;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
unset($summary[2]);
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$fraction_format = $this
->getSetting('fraction_format');
$unit_list = $this->ingredientUnitUtility
->getConfiguredUnits();
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $entity) {
$name = Xss::filter($entity
->label(), []);
$note = Xss::filter($items[$delta]->note, []);
if ($items[$delta]->quantity > 0) {
$formatted_quantity = $this->ingredientQuantityUtility
->getQuantityFromDecimal($items[$delta]->quantity, $fraction_format);
}
else {
$formatted_quantity = ' ';
}
$unit_name = '';
$unit_abbreviation = '';
$unit = isset($unit_list[$items[$delta]->unit_key]) ? $unit_list[$items[$delta]->unit_key] : [];
if (!empty($unit['abbreviation'])) {
$unit_name = $items[$delta]->quantity > 1 ? $unit['plural'] : $unit['name'];
$unit_abbreviation = $unit['abbreviation'];
}
$elements[$delta] = [
'#theme' => 'ingredient_recipeml_formatter',
'#name' => $name,
'#quantity' => $formatted_quantity,
'#unit_name' => $unit_name,
'#unit_abbreviation' => $unit_abbreviation,
'#unit_display' => $this
->getSetting('unit_abbreviation'),
'#note' => $note,
];
}
return $elements;
}
}