11

Using the group module in D8, I have the below view with machine name: group_members_per_group

group members per group

Machine Name of:

  • (Member account) User: Full Name is: field_user_full_name
  • (Member account) User: Phone Number is: field_user_phone_number

The above view has the below results:

View results

On cron run, and while Looping through all rows of the view results, I want to get for each row the field_user_phone_number value so I can use it with other code.

So I wrote:

function my_module_cron() {

// Get and loop through the View `group_members_per_group`
//$args = [$gid];
$view = \Drupal\views\Views::getView('group_members_per_group');
//$view->setArguments($args);
$view->setDisplay('default');
$view->execute();

// Get the results of the view.
$view_result = $view->result;

// Check if the view is not empty and return results.
if (!empty($view_result)) {

// If the view returns results...
foreach ($view->result as $row) {

// Get the full name value.
$name = $row->field_user_full_name;


// check the result output for testing only.
\Drupal::messenger()->addMessage(t($name));
  }
 }
}

However, on cron run, I am receiving the below error:

Notice: Undefined property: Drupal\views\ResultRow::$field_user_full_name in my_module_cron() (line 103 of modules\custom\my_module\my_module.module). my_module_cron(Object) call_user_func('my_module_cron', Object) (Line: 316) Drupal\ultimate_cron\Entity\CronJob->invokeCallback() (Line: 459) Drupal\ultimate_cron\Entity\CronJob->run(Object) (Line: 24) Drupal\ultimate_cron\Controller\JobController->runCronJob(Object) call_user_func_array(Array, Array) (Line: 123) Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 582) … …

Line 103 in the error is:

$name = $row->field_user_full_name;
2
  • 2
    You need to combine $view->result with $view->field, see drupal.stackexchange.com/questions/219475/…
    – 4uk4
    Commented May 3, 2019 at 20:29
  • @4k4 I ran through this link while searching online for answers, but did not understand how to use as for my use case! Can you please give a real example on how to use this combination ?
    – user93333
    Commented May 3, 2019 at 20:45

3 Answers 3

7

You can not get the value of field directly from row result.
You can get it by accessing to the entity like the following:
Option1

  foreach ($view->result as $id => $result) {
    $node = $result->_entity;
    // Get the full name value.
    $name = $node->get('field_user_full_name')->value;
    // check the result output for testing only.
    \Drupal::messenger()->addMessage(t($name));
  }

Or by loop through the fields like the following:
Option2:

foreach ($view->result as $id => $row) {
      foreach ($view->field as $fid => $field) {
        if ($fid == 'field_user_full_name') {
          //Get the full name value.
          $name = $field->getValue($row);
          // check the result output for testing only.
          \Drupal::messenger()->addMessage(t($name));
        }
      }
    }
4
  • 1
    Also, wrong, as are all the answers here. These are simply describing how to get a field from the base entity returned by the View. These are not the fields of the view.
    – liquidcms
    Commented Dec 15, 2021 at 18:11
  • 1
    if you find those answers wrong, step up and post one correct people here just trying to help so they don't need those kind of comments
    – berramou
    Commented Dec 16, 2021 at 13:08
  • that's an odd comment. I certainly appreciate it when people post that something is wrong; that way i don't waste time trying it (as i did with all these answers). And knowing something is wrong doesn't mean you know the right answer - i am still looking.
    – liquidcms
    Commented Dec 16, 2021 at 15:45
  • posted correct answer below.
    – liquidcms
    Commented Dec 16, 2021 at 16:18
4

In my case, the '$result->_entity' was empty, so I had to get the value from the '$result->_object' :

function YOUR_THEME_preprocess_views_view__YOUR_VIEW_ID(array &$variables): void {
  foreach ($variables['view']->result as $result) {
    $node = $result->_object->getEntity();
    $title = $node->get('title')->getValue()[0]['value'];
    $body = $node->get('body')->getValue()[0]['value'];
  }
}
2
  • Thanks, that helps a lot Commented Jan 24, 2020 at 10:54
  • 1
    In fact, $result->_entity is empty when the preprocessed view is rendering data from a Search API index and not rendering standard content.
    – Roger
    Commented Aug 23, 2021 at 17:56
3

I had to re-ask this question to be more clear here: How to get a rendered views field value programmatically?

@4k4 pointed me towards the right solution for getting a value from a field in a view:

foreach ($view->result as $rid => $row) {
  $type[$row->nid] = $view->field['my_views_field']->advancedRender($row)->__toString();
}
2
  • Hmm, odd this is down-voted when no reason added. Not to mention it is the correct answer.
    – liquidcms
    Commented Dec 16, 2021 at 21:18
  • 1
    Error: Call to a member function __toString() on string
    – Mike Godin
    Commented Apr 26, 2022 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.