Commit 250b988b authored by Leonardo Alv's avatar Leonardo Alv
Browse files

Issue #3317476: Create the video edit page

parent e40a78ca
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -31,7 +31,13 @@ class VideoToolBoxReportController extends ControllerBase {
    ];
    $videos = \Drupal::service('video.get_service')->getVideosInfo();
    $rows = [];

    foreach ($videos as $entry) {

      if (strlen($entry['description']) > 20) {
        $entry['description'] = substr($entry['description'], 0, 20) . '...';
      }

      $url = Url::fromRoute('video_toolbox.videos_url', ['video_id' => $entry['video_key']])->toString();
      $rows[] = [
        $entry['name'],
+6 −1
Original line number Diff line number Diff line
@@ -65,6 +65,12 @@ class VideoGetUploaderForm extends FormBase {
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $videogetter = \Drupal::service('video.get_service');
    $key = $form_state->getValue('key');
    $check = $videogetter->getVideoInfo($key);
    if ($check != []) {
      $form_state->setErrorByName('key', $this->t('This key is being used'));
    }

  }

@@ -87,4 +93,3 @@ class VideoGetUploaderForm extends FormBase {
  }

}
+128 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\video_toolbox\Form;

use Drupal\file\Entity\File;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a Video toolbox form.
 */
class VideosUrlForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'video_default_url';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $video_id = NULL) {

    $videogetter = \Drupal::service('video.get_service');
    $videos = $videogetter->getVideoInfo($video_id);

    $current_user = \Drupal::currentUser();
    $uid = $current_user->id();
    $roles = $current_user->getRoles();

    if ($videos[0]['uid'] != $uid || !array_search("administrator", $roles)) {

      $file = File::load($videos[0]['fid']);
      $videourl = $file->createFileUrl();
      $form['video' . $video_id] = [
        '#type' => 'html_tag',
        '#tag' => 'iframe',
        '#attributes' => [
          'src' => $videourl,
          'allow' => 'fullscreen',
          'width' => 420,
          'height' => 315,
          'loading' => 'eager',
        ],
      ];
      return $form;
    }

    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save'),
    ];

    $form['download'] = [
      '#type' => 'button',
      '#value' => $this->t('Download'),
      '#submit' => ['downloadFunc'],
    ];

    $form['description'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Description'),
      '#required' => TRUE,
      '#default_value' => $videos[0]['description'],
    ];

    $file = File::load($videos[0]['fid']);
    $videourl = $file->createFileUrl();
    $form['video' . $video_id] = [
      '#type' => 'html_tag',
      '#tag' => 'video',
      '#attributes' => [
        'src' => $videourl,
        'allow' => 'fullscreen',
        'width' => 420,
        'height' => 315,
        'loading' => 'eager',
        'controls' => 'true',
      ],
    ];

    $form['fid'] = [
      '#type' => 'hidden',
      '#value' => $videos[0]['fid'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $desc = $form_state->getValue('description');
    if (is_string($desc) && mb_strlen($desc) < 10) {
      $form_state->setErrorByName('description', $this->t('Description should be at least 10 characters.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $desc = $form_state->getValue('description');
    $fid = $form_state->getValue('fid');
    $videogetter = \Drupal::service('video.get_service');
    $videogetter->updateVideos($desc, $fid);

    $this->messenger()->addStatus($this->t('Description saved'));
  }

  /**
   * Download video.
   */
  public function downloadFunc(FormStateInterface $form_state) {

    $desc = $form_state->getValue('description');
    $fid = $form_state->getValue('fid');
    $videogetter = \Drupal::service('video.get_service');
    $videogetter->updateVideos($desc, $fid);

    $this->messenger()->addStatus($this->t('Description saved'));
  }

}
+12 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ class VideoHandler implements VideoHandlerInterface {
  }

  /**
   * Get the video.
   * Get the videos.
   */
  public function getVideosInfo() {
    $query = \Drupal::database()->select('video_store', 'vs');
@@ -58,4 +58,15 @@ class VideoHandler implements VideoHandlerInterface {
    return $results;
  }

  /**
   * Update metadata.
   */
  public function updateVideos($description, $fid) {
    $query = \Drupal::database()->update('video_store');
    $query->condition('fid', [(string) $fid]);
    $query->fields(['description' => (string) $description]);
    $query->execute();

  }

}
+13 −0
Original line number Diff line number Diff line
@@ -23,3 +23,16 @@ video_toolbox.report:
    _title: 'List of uploaded videos'
  requirements:
    _role: 'administrator'

video_toolbox.videos_url:
  path: '/video/{video_id}/edit'
  defaults:
    _title: 'Modify video'
    _form: 'Drupal\video_toolbox\Form\VideosUrlForm'
  requirements:
    _role: 'authenticated'
  options:
    parameters:
      video_id:
        type: string