mutual_credit-5.0.x-dev/src/Plugin/Field/CurrencyComputedProperty.php

src/Plugin/Field/CurrencyComputedProperty.php
<?php

namespace Drupal\mcapi\Plugin\Field;

use Drupal\mcapi\Entity\Currency;
use Drupal\Core\Field\EntityReferenceFieldItemList;

/**
 * A computed property for the loaded currency in a worth field.
 *
 * Required settings (below the definition's 'settings' key) are:
 *  - currency source: The currency.
 */
class CurrencyComputedProperty extends EntityReferenceFieldItemList {

  /**
   * Cached computed currency.
   *
   * @var \Currency|null
   */
  protected $currency = NULL;


  /**
   * {@inheritdoc}
   */
  public function getValue($langcode = NULL) : Currency {
    if (empty($this->currency)) {
      $curr_id = $this->getParent()->getValue()['curr_id'];
      $this->currency = Currency::load($curr_id);
    }
    return $this->currency;
  }

  /**
   * {@inheritdoc}
   */
  public function setValue($value, $notify = TRUE) {
    $this->currency = $value;
    // Notify the parent entity of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent->onChange('worth');
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc