protected function SimpleMathField::getFieldValue in Views Simple Math Field 8
Same name and namespace in other branches
- 8.2 src/Plugin/views/field/SimpleMathField.php \Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField::getFieldValue()
Get the value of a simple math field.
Parameters
ResultRow $values: Row results.
\Drupal\Core\Entity\EntityInterface|null $entity: The current row entity.
bool $fieldOne: Whether we are fetching field one's value.
Return value
mixed The field value.
Throws
\Exception
1 call to SimpleMathField::getFieldValue()
- SimpleMathField::getValue in src/
Plugin/ views/ field/ SimpleMathField.php
File
- src/
Plugin/ views/ field/ SimpleMathField.php, line 265 - Defines Drupal\views_simple_math_field\Plugin\views\field\SimpleMathField.
Class
- SimpleMathField
- Field handler to complete mathematical operation.
Namespace
Drupal\views_simple_math_field\Plugin\views\fieldCode
protected function getFieldValue(ResultRow $values, $entity, $fieldOne) {
if (!empty($fieldOne)) {
$field = $this->options['fieldset_one']['data_field_one'];
}
else {
$field = $this->options['fieldset_two']['data_field_two'];
}
// Determine what type of field is being used.
$field_type = $this
->getFieldType($field);
// If the field is rewritten, get the rewritten text. Else, returns null.
$rewritten = $this
->getRewriteStatus($field);
$data = NULL;
// Process if not a Views Simple Math field constant.
if ($field !== 'const') {
// Process if not undefined.
if ($field_type !== 'undefined') {
// Get the value of a field that comes from a relationship.
$relationship = $this
->getFieldRelationship($field);
if ($relationship) {
// Use the relationship's entity to fetch the field value.
$entity = $this
->getRelationshipEntity($values, $field, $relationship);
}
// The next two statements handle fields with or without a relationship.
if (isset($entity) && $rewritten) {
// If already numeric, there is no need for advancedRender().
if (is_numeric($rewritten) == TRUE) {
$data = $rewritten;
}
else {
$data = $this->view->field[$field]
->advancedRender($values);
}
}
if (isset($entity) && !$rewritten) {
$field_base = $this->displayHandler
->getHandler('field', $field)->field;
if ($entity
->hasField($field_base)) {
$data = $entity
->get($field_base)
->getValue()[0]['value'];
}
// For Commerce fields.
if ($field_type === 'commerce_price_default' || $field_type === 'commerce_product_variation') {
$commerce_field_id = $this->displayHandler
->getHandler('field', $field)->options['id'];
if ($commerce_field_id === 'list_price__number') {
$data = $entity
->get('list_price')
->getValue()[0]['number'];
}
if ($commerce_field_id === 'price__number') {
$data = $entity
->get('price')
->getValue()[0]['number'];
}
}
}
}
else {
$data = $this->view->field[$field]
->getValue($values);
if ($rewritten) {
if (\Drupal::routeMatch()
->getRouteName() == 'entity.view.preview_form') {
\Drupal::service('messenger')
->addMessage(t('Views Simple Math Field sometimes has difficulty rendering the correct value for rewritten fields. You may want to double check that field ID <em>@field</em> is properly outputting a value.', [
'@field' => $field,
]), 'warning');
}
// Use current() to ensure result is a string.
$data = current($this->displayHandler
->getHandler('field', $field)
->advancedRender($values));
return $data;
}
// If a Views Simple Math Field, ensure the value is numeric.
if (strpos($field, 'field_views_simple_math_field') === 0) {
// If a separator is used, remove it before performing the math.
if (!empty($this->options['separator'])) {
$data = $this->view->field[$field]
->getValue($values);
$separator = $this->options['separator'];
if (strpos($data, $separator)) {
$data = str_replace($separator, '', $data);
}
}
else {
$data = $this->view->field[$field]
->getValue($values);
if (strpos($data, ',')) {
$data = str_replace(',', '', $data);
}
if (strpos($data, ' ')) {
$data = str_replace(' ', '', $data);
}
}
}
}
}
else {
if (!empty($fieldOne)) {
$data = $this->options['fieldset_one']['constant_one'];
}
else {
$data = $this->options['fieldset_two']['constant_two'];
}
}
// There's no value. Default to 0.
if (!isset($data)) {
$data = 0;
}
return $data;
}