You are here

function easychart_entity_presave in Easychart 8.3

Implements hook_entity_presave().

Copies the Easychart icon in case the chart embed button is installed.

File

./easychart.module, line 100
Easychart module file.

Code

function easychart_entity_presave(EntityInterface $entity) {

  // Install the chart file.
  if ($entity
    ->getEntityTypeId() == 'embed_button' && $entity
    ->id() == 'chart' && $entity
    ->isNew()) {
    $path = drupal_get_path('module', 'easychart') . '/assets/chart.png';

    /* @var $file \Drupal\file\FileInterface */
    $file = File::create();
    $file
      ->setFileUri($path);
    $file
      ->setOwnerId(\Drupal::currentUser()
      ->id());
    $file
      ->setMimeType('image/' . pathinfo($path, PATHINFO_EXTENSION));
    $file
      ->setFileName(\Drupal::service('file_system')
      ->basename($path));
    $chart_icon = file_copy($file, 'public://embed_buttons');
    if ($chart_icon) {
      $entity->icon_uuid = $chart_icon
        ->uuid();
    }
  }
}