You are here

function quantity_measure_default_value in farmOS 2.x

Sets the default value for the quantity measure field.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity being created.

\Drupal\Core\Field\FieldDefinitionInterface $definition: The field definition.

Return value

array An array of default value keys with each entry keyed with the “value” key.

See also

\Drupal\Core\Field\FieldConfigBase::getDefaultValue()

File

modules/core/quantity/quantity.module, line 100
Quantity module.

Code

function quantity_measure_default_value(ContentEntityInterface $entity, FieldDefinitionInterface $definition) : array {

  // Defaults to an empty array.
  $default = [];

  // Get the quantity type default measure.

  /** @var \Drupal\quantity\Entity\QuantityTypeInterface $quantity_type */
  $quantity_type = $entity
    ->get('type')->entity;
  $measure = $quantity_type
    ->getDefaultMeasure();

  // Only use the measure if not empty.
  if (!empty($measure)) {
    $default[] = [
      'value' => $measure,
    ];
  }
  return $default;
}