protected function SimpleMathField::getFieldValue in Views Simple Math Field 8.2
Same name and namespace in other branches
- 8 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
\Drupal\views\ResultRow $values: Row results.
\Drupal\Core\Entity\EntityInterface|null $entity: The current row entity.
bool $field: The field we are fetching.
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 282 - 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, $field) {
// Determine what type of field is being used.
$field_type = $this
->getFieldType($field);
// Determine what type of field plugin is being used.
$field_plugin = $this
->getFieldPlugin($field);
// If the field is rewritten, get the rewritten text. Else, returns null.
$rewritten = $this
->getRewriteStatus($field);
// Check if the display is aggregated. Not needed right now.
// $isAggregated = $this->view->getDisplay()->getOption('group_by');
$data = NULL;
// Compatibility with the views_entity_form_field module.
if ($field_plugin === 'entity_form_field') {
$field_handler = $this->displayHandler
->getHandler('field', $field)->options;
if (!empty($field_handler['plugin']['type'])) {
$field_type = $field_handler['plugin']['type'];
}
if (!empty($field_handler['field'])) {
$form_field = $field_handler['field'];
$prefix = 'form_field_';
if (0 === strpos($form_field, $prefix)) {
$field = substr($form_field, strlen($prefix));
}
}
$relationship = $this
->getFieldRelationship($field);
if ($relationship) {
// Use the relationship's entity to fetch the field value.
$entity = $this
->getRelationshipEntity($values, $field, $relationship);
}
// For Commerce fields.
if (in_array($field_type, $this->commerce_price_fields)) {
$commerce_field_id = $this->displayHandler
->getHandler('field', 'form_field_' . $field)->options['id'];
if ($entity
->hasField($commerce_field_id) && !empty($entity
->get($commerce_field_id)
->getValue())) {
$data = $entity
->get($commerce_field_id)
->first()
->toPrice();
}
}
else {
$data = $entity
->get($field)
->getValue()[0]['value'];
}
return $data;
}
// 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 {
// @todo: rewrite using dependency injection.
if (\Drupal::routeMatch()
->getRouteName() == 'entity.view.preview_form') {
\Drupal::service('messenger')
->addMessage(t('It appears that <em>@field</em> is rewritten and requires advanced rendering. Do not use the Views Simple Math Field sort handler for this View.', [
'@field' => $field,
]), 'warning');
}
$data = $this->view->field[$field]
->advancedRender($values);
}
}
if (isset($entity) && !$rewritten) {
// Gets the value from the row, which works when aggregated (or not).
$data = $this->view->field[$field]
->getValue($values);
/**
* Keeping these here, but I've commented out in favor of the above
* line, as this was not working with aggregation.
* $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 (in_array($field_type, $this->commerce_price_fields)) {
$commerce_field_id = $this->displayHandler
->getHandler('field', $field)->options['id'];
if ($entity
->hasField($commerce_field_id) && !empty($entity
->get($commerce_field_id)
->getValue())) {
$data = $entity
->get($commerce_field_id)
->first()
->toPrice();
}
}
}
}
else {
if (isset($this->view->field[$field]->original_value)) {
$data = $this->view->field[$field]->original_value;
}
else {
$data = $this->view->field[$field]
->getValue($values);
}
if ($rewritten) {
// @todo: rewrite using dependency injection.
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');
}
$data = $this->displayHandler
->getHandler('field', $field)
->advancedRender($values);
}
}
// There's no value. Default to 0.
if (!isset($data)) {
$data = 0;
}
// Remove the thousands marker.
$data = $this
->removeSeparator($field, $data);
return $data;
}