You are here

class N1edFileField in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Hierarchy

Expanded class hierarchy of N1edFileField

File

src/N1edFileField.php, line 9

Namespace

Drupal\n1ed
View source
class N1edFileField implements TrustedCallbackInterface {
  public static function trustedCallbacks() {
    return [
      'preRenderWidget',
    ];
  }

  /**
   * Processes widget form.
   */
  public static function processWidget($element, FormStateInterface $form_state, $form) {
    $settings = \Drupal::config('n1ed.settings');
    $apiKey = $settings
      ->get('apikey') ?: 'N1D8DFLT';
    $version = $settings
      ->get('version') ?: '';
    if ($version === '') {
      $version = 'latest';
    }
    $urlCache = $settings
      ->get('urlCache') ?: '';
    if ($urlCache === '') {

      // Fix for: https://www.drupal.org/project/n1ed/issues/3111919
      // Do not start URL with "https:" prefix.
      // Notice about cookies: developers use it to specify debug server to use,
      // all other users will use old known cloud.n1ed.com address
      $standaloneSrc = '//' . (isset($_COOKIE['N1ED_PREFIX']) ? $_COOKIE['N1ED_PREFIX'] . '.' : '') . 'cloud.n1ed.com/cdn/' . $apiKey . '/' . 'n1flmngr.js';
      $standaloneImgPen = '//' . (isset($_COOKIE['N1ED_PREFIX']) ? $_COOKIE['N1ED_PREFIX'] . '.' : '') . 'cloud.n1ed.com/cdn/' . $apiKey . '/' . 'n1imgpen.js';
    }
    else {

      // Fix for: https://www.drupal.org/project/n1ed/issues/3111919
      // Do not start URL with "https:" prefix.
      if (strpos($urlCache, 'http:') === 0) {
        $urlCache = substr($urlCache, 5);
      }
      elseif (strpos($urlCache, 'https:') === 0) {
        $urlCache = substr($urlCache, 6);
      }
      $standaloneSrc = $urlCache . $apiKey . '/' . 'n1flmngr.js';
      $standaloneSrc = $urlCache . $apiKey . '/' . 'n1imgpen.js';
    }
    $standaloneUrl = Url::fromRoute('n1ed.flmngr')
      ->toString();
    $standaloneCSRF = 'drupal8';

    // Get path to /sites/SITE/files/flmngr
    $standaloneUrlFiles = parse_url(file_create_url('public://flmngr'))['path'];
    $standaloneDirUploads = '/';
    if ($settings
      ->get('selfHosted')) {
      $standaloneSrc = selfHostedPath . 'Flmngr/flmngr.js';
      $standaloneImgPen = selfHostedPath . 'ImgPen/imgpen.js';
    }
    $standaloneData = [
      'standaloneSrc' => $standaloneSrc,
      'standaloneUrl' => $standaloneUrl,
      'standaloneCSRF' => $standaloneCSRF,
      'standaloneUrlFiles' => $standaloneUrlFiles,
      'standaloneDirUploads' => $standaloneDirUploads,
      'standaloneimgpen' => $standaloneImgPen,
    ];
    if ($settings
      ->get('selfHosted')) {
      $standaloneData['apikey'] = $apiKey;
    }
    $element['n1ed_paths'] = [
      '#type' => 'hidden',
      '#attributes' => [
        'class' => [
          'flmngrWidget-filefield-paths',
        ],
        'data-extensions' => $element['#upload_validators']['file_validate_extensions'][0],
        'data-standaloneData' => json_encode($standaloneData),
        'data-multiple' => $element['#multiple'] ? 1 : 0,
      ],
      // Reset value to prevent consistent errors.
      '#value' => '',
    ];

    // Library.
    $element['#attached']['library'][] = 'n1ed/drupal.n1ed.filefield';

    // Set the pre-renderer to conditionally disable the elements.
    $element['#pre_render'][] = [
      get_called_class(),
      'preRenderWidget',
    ];
    return $element;
  }

  /**
   * Pre-renders widget form.
   */
  public static function preRenderWidget($element) {

    // Hide elements if there is already an uploaded file.
    if (!empty($element['#value']['fids'])) {
      $element['n1ed_paths']['#access'] = false;
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
N1edFileField::preRenderWidget public static function Pre-renders widget form.
N1edFileField::processWidget public static function Processes widget form.
N1edFileField::trustedCallbacks public static function Lists the trusted callbacks provided by the implementing class. Overrides TrustedCallbackInterface::trustedCallbacks
TrustedCallbackInterface::THROW_EXCEPTION constant Untrusted callbacks throw exceptions.
TrustedCallbackInterface::TRIGGER_SILENCED_DEPRECATION constant Untrusted callbacks trigger silenced E_USER_DEPRECATION errors.
TrustedCallbackInterface::TRIGGER_WARNING constant Untrusted callbacks trigger E_USER_WARNING errors.