1

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
    You need to combine $view->result with $view->field, see drupal.stackexchange.com/questions/219475/… – 4k4 May 3 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 May 3 at 20:45
1

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));
        }
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy