You are here

protected function MediaWatermarkForm::addFileUsage in Media watermark 8

Helper to add file usage.

Parameters

\Drupal\Core\Form\FormStateInterface $form_state: An associative array containing the current state of the form.

1 call to MediaWatermarkForm::addFileUsage()
MediaWatermarkForm::save in src/Form/MediaWatermarkForm.php
Form submission handler for the 'save' action.

File

src/Form/MediaWatermarkForm.php, line 181

Class

MediaWatermarkForm
Class MediaWatermarkForm.

Namespace

Drupal\media_watermark\Form

Code

protected function addFileUsage(FormStateInterface $form_state) {
  $media_watermark = $this->entity;
  $id = $media_watermark
    ->id();
  $fids = $form_state
    ->getValue('fid');
  if (!empty($fids) && is_array($fids)) {
    $fid = reset($fids);

    // Process file only if it is not already part of.
    if (!empty($fid) && is_numeric($fid)) {
      $file = File::load($fid);

      // Add file usage.
      \Drupal::service('file.usage')
        ->add($file, 'media_watermark', 'media_watermark', $id);
    }
  }
}