Quantcast
Channel: Active questions tagged cron - Drupal Answers
Viewing all articles
Browse latest Browse all 37

Programmatically get the results of a view

$
0
0

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;

Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>