NULL, 'weight_max' => NULL, ] + parent::defaultConfiguration(); } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); $weight_min = $this->configuration['weight_min']; $weight_max = $this->configuration['weight_max']; $form['weight_min'] = [ '#type' => 'physical_measurement', '#measurement_type' => MeasurementType::WEIGHT, '#title' => $this->t('Minimum Weight'), '#default_value' => $weight_min, '#required' => TRUE, ]; $form['weight_max'] = [ '#type' => 'physical_measurement', '#measurement_type' => MeasurementType::WEIGHT, '#title' => $this->t('Maximum Weight'), '#default_value' => $weight_max, '#required' => TRUE, ]; return $form; } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); $values = $form_state->getValue($form['#parents']); $this->configuration['weight_min'] = $values['weight_min']; $this->configuration['weight_max'] = $values['weight_max']; } /** * {@inheritdoc} */ public function evaluate(EntityInterface $entity) { $shipment = $entity; $this->assertEntity($entity); /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */ $shipment = $entity; $weight = $shipment->getWeight(); if (!$weight) { // The conditions can't be applied until the weight is known. return FALSE; } $condition_unit_min = $this->configuration['weight_min']['unit']; $condition_unit_max = $this->configuration['weight_max']['unit']; /** @var \Drupal\physical\Weight $weight */ $weight_min = $weight->convert($condition_unit_min); $weight_max = $weight->convert($condition_unit_max); $condition_weight_min = new Weight($this->configuration['weight_min']['number'], $condition_unit_min); $condition_weight_max = new Weight($this->configuration['weight_max']['number'], $condition_unit_max); if ($weight_min->greaterThanOrEqual($condition_weight_min) && $weight_max->lessThanOrEqual($condition_weight_max)) { return TRUE; } else { return FALSE; } } }