Commit e091ab3a authored by Quang-Minh DANG's avatar Quang-Minh DANG
Browse files

Issue #3323389 by Dakwamine: Integration with Weight module

parent df97ee09
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
name: 'Decoupled Toolbox for Weight'
type: module
description: 'Makes Weight compatible with Decoupled Toolbox.'
core_version_requirement: ">=8"
package: 'Decoupled'
dependencies:
  - decoupled_toolbox:decoupled_toolbox
  - weight:weight
+34 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\decoupled_toolbox_weight\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemInterface;
use Drupal\decoupled_toolbox\Exception\InvalidContentException;
use Drupal\decoupled_toolbox\Plugin\Field\FieldFormatter\GenericDecoupledFormatter;
use Drupal\weight\Plugin\Field\FieldType\WeightItem;

/**
 * Plugin implementation of the 'decoupled_weight' formatter.
 *
 * @FieldFormatter(
 *   id = "decoupled_weight",
 *   label = @Translation("Weight decoupled formatter"),
 *   field_types = {
 *     "weight",
 *   }
 * )
 */
class WeightDecoupledFormatter extends GenericDecoupledFormatter {

  /**
   * {@inheritdoc}
   */
  protected function viewFieldItem(FieldItemInterface $item) {
    if (!$item instanceof WeightItem) {
      throw new InvalidContentException($this->t('Not a valid weight field item.'));
    }

    return (int) $item->getString();
  }

}