You are here

public static function EasychartUpdate::updateCSVFromUrl in Easychart 8.3

Update the csv data from the url stored in the database.

1 call to EasychartUpdate::updateCSVFromUrl()
easychart_cron in ./easychart.module
Implements hook_cron().

File

src/EasychartUpdate.php, line 15
Contains \Drupal\easychart\EasychartUpdate

Class

EasychartUpdate

Namespace

Drupal\easychart

Code

public static function updateCSVFromUrl() {
  $field_storages = \Drupal::entityTypeManager()
    ->getStorage('field_storage_config')
    ->loadByProperties(array(
    'type' => 'easychart',
  ));

  /* @var $field_storage \Drupal\field\FieldStorageConfigInterface */
  foreach ($field_storages as $field_storage) {
    $field_name = $field_storage
      ->getName();
    $entity_type = $field_storage
      ->getTargetEntityTypeId();
    $ids = \Drupal::entityQuery($entity_type)
      ->condition($field_name . '.csv_url', "", "!=")
      ->execute();
    if (!empty($ids)) {
      $entities = \Drupal::entityTypeManager()
        ->getStorage($entity_type)
        ->loadMultiple($ids);

      /* @var $entity \Drupal\Core\Entity\EntityInterface */
      foreach ($entities as $entity) {
        $url = $entity->{$field_name}->csv_url;
        $csv_data = file_get_contents($url);
        if (!empty($csv_data)) {
          $delimiter = static::findCSVDelimiter($csv_data);
          $csv = json_encode(static::parseCSV($csv_data, $delimiter));
          $entity->{$field_name}->csv = $csv;
          $entity
            ->save();
        }
      }
    }
  }
}