<?php

/**
 * @file
 */

use Drupal\node\Entity\Node;
use Drupal\Core\Entity\EntityInterface;
/**
 * Implements hook_cron().
 */

/*function getResult($queue,$url){

  //do {
  $request = \Drupal::service('myservice.my')->getResult($url);

  $result = $request->results; // выкинул служебную информацию
    foreach ($result as $row_item){
      $queue->createItem($row_item);
    }

  //$url = $request->next;

//} while ($url);

 return $queue;
}

/*

/*function getNodePeople($queue){

  $end = time() + 30;
  while (time() < $end && ($item = $queue->claimItem())) {

    $fieldurl = (string)$item->data->url;
    preg_match_all("/\d+/", $fieldurl, $counter);


    # Данные которые мы добавляли в очередь находятся в $item->data.
    $node = Node::create(array(
      'type' => 'people' ,
      'title' => 'people' . $counter[0][0],
      'field_name' =>$item->data->name,
      'field_height' =>$item->data->height,
      'field_mass' =>$item->data->mass,
      'field_hair_color' =>$item->data->hair_color,
      'field_skin_color' =>$item->data->skin_color,
      'field_eye_color' =>$item->data->eye_color,
      'field_birth_year' =>$item->data->birth_year,
      'field_gender' =>$item->data->gender,
      'langcode' => 'ru',
      'status' => 1,
    ));
    $node->save();
    $queue->deleteItem($item); //удаление элементов
  }
  $queue->deleteQueue(); //удаление очереди
}
*/
function myservice_cron()

{
  $queue = \Drupal::queue('my_test_queue'); //объявляю очередь
  $queue->createQueue();
  $request = \Drupal::service('myservice.my')->getResult('https://swapi.dev/api/');
 // $result = $request->results; // выкинул служебную информацию
  foreach ($request as $row_item){
    $queue->createItem($row_item);
  }

  //getResult($queue,'https://swapi.dev/api/');
  //getNodePeople($queue);
  /**
   * Implements hook_form_alter().
   */

}

function myservice_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'contact_message_contact_form_form') {
    $current_user = \Drupal::currentUser();
    $user = \Drupal\user\Entity\User::load($current_user->id());
    $name  = $user->name->value;
    $user_last_name =  $user->field_last_name->value;
    $form['field_last_name']['widget'][0]['value']['#default_value'] = $user_last_name;
    $form['field_name']['widget'][0]['value']['#default_value'] = $name;
  }

}


//$name = $user['values']['name'];
/* $users = \Drupal::entityTypeManager()
   ->getStorage('user')
   ->loadByProperties([
     'name' => $name,
   ]);
foreach ($users as $concrete_user){
$user = $concrete_user;
}
*/
