Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public static function process($element, FormStateInterface $form_state, $form)
 {
     $item = $element['#value'];
     $element['slide_text'] = array('#type' => 'textarea', '#title' => t('Slide Text'), '#default_value' => isset($element['#default_value']['slide_text']) ? $element['#default_value']['slide_text'] : '', '#maxlength' => 1024, '#weight' => -2);
     $element = parent::process($element, $form_state, $form);
     return $element;
 }
 /**
  * {@inheritDocs}
  *
  * Form API callback: Processes a image_fp field element.
  *
  * Expands the image_fp type to include the focal_point field.
  *
  * This method is assigned as a #process callback in formElement() method.
  */
 public static function process($element, FormStateInterface $form_state, $form)
 {
     $element = parent::process($element, $form_state, $form);
     $item = $element['#value'];
     $item['fids'] = $element['fids']['#value'];
     // Add the focal point indicator to preview.
     if (isset($element['preview'])) {
         $preview = array('indicator' => array('#theme_wrappers' => array('container'), '#attributes' => array('class' => array('focal-point-indicator'), 'data-field-name' => $element['#field_name'], 'data-delta' => $element['#delta']), '#markup' => ''), 'thumbnail' => $element['preview']);
         // Use the existing preview weight value so that the focal point indicator
         // and thumbnail appear in the correct order.
         $preview['#weight'] = isset($element['preview']['#weight']) ? $element['preview']['#weight'] : 0;
         unset($preview['thumbnail']['#weight']);
         $element['preview'] = $preview;
     }
     // Add the focal point field.
     $element['focal_point'] = array('#type' => 'textfield', '#title' => 'Focal point', '#description' => t('Specify the focus of this image in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75'), '#default_value' => isset($item['focal_point']) ? $item['focal_point'] : FocalPoint::DEFAULT_VALUE, '#element_validate' => array('\\Drupal\\focal_point\\Plugin\\Field\\FieldWidget\\FocalPointImageWidget::validateFocalPoint'), '#attributes' => array('class' => array('focal-point', 'focal-point-' . $element['#field_name'] . '-' . $element['#delta']), 'data-delta' => $element['#delta'], 'data-field-name' => $element['#field_name']), '#attached' => array('library' => array('focal_point/drupal.focal_point')));
     return $element;
 }
Ejemplo n.º 3
0
  /**
   * {@inheritdoc}
   *
   * Form API callback: Processes an image_fp field element.
   *
   * Expands the image_fp type to include the focal_point field.
   *
   * This method is assigned as a #process callback in formElement() method.
   */
  public static function process($element, FormStateInterface $form_state, $form) {
    $element = parent::process($element, $form_state, $form);

    $item = $element['#value'];
    $item['fids'] = $element['fids']['#value'];
    $element_selector = 'focal-point-' . implode('-', $element['#parents']);

    $default_focal_point_value = isset($item['focal_point']) ? $item['focal_point'] : \Drupal::config('focal_point.settings')->get('default_value');

    // Add the focal point indicator to preview.
    if (isset($element['preview'])) {
      $indicator = array(
        '#theme_wrappers' => array('container'),
        '#attributes' => array(
          'class' => array('focal-point-indicator'),
          'data-selector' => $element_selector,
          'data-delta' => $element['#delta'],
        ),
        '#markup' => '',
      );

      $preview = array(
        'indicator' => $indicator,
        'thumbnail' => $element['preview'],
      );

      $display_preview_link = \Drupal::config('focal_point.preview')->get('display_link');

      // Even for image fields with a cardinality higher than 1 the correct fid
      // can always be found in $item['fids'][0].
      $fid = isset($item['fids'][0]) ? $item['fids'][0] : '';
      if ($display_preview_link && !empty($fid)) {
        // Replace comma (,) with an x to make javascript handling easier.
        $preview_focal_point_value = str_replace(',', 'x', $default_focal_point_value);

        // Create a token to be used during an access check on the preview page.
        $token = self::getPreviewToken();

        $preview_link = [
          '#type' => 'link',
          '#title' => t('Preview'),
          '#url' => new Url('focal_point.preview',
            [
              'fid' => $fid,
              'focal_point_value' => $preview_focal_point_value,
            ],
            [
              'query' => array('focal_point_token' => $token),
            ]),
          '#attributes' => [
            'class' => array('focal-point-preview-link'),
            'data-selector' => $element_selector,
            'data-field-name' => $element['#field_name'],
            'target' => '_blank',
          ],
        ];

        $preview['preview_link'] = $preview_link;
      }

      // Use the existing preview weight value so that the focal point indicator
      // and thumbnail appear in the correct order.
      $preview['#weight'] = isset($element['preview']['#weight']) ? $element['preview']['#weight'] : 0;
      unset($preview['thumbnail']['#weight']);

      $element['preview'] = $preview;
    }

    // Add the focal point field.
    $element_selector = 'focal-point-' . implode('-', $element['#parents']);
    $element['focal_point'] = array(
      '#type' => 'textfield',
      '#title' => 'Focal point',
      '#description' => new TranslatableMarkup('Specify the focus of this image in the form "leftoffset,topoffset" where offsets are in percents. Ex: 25,75'),
      '#default_value' => $default_focal_point_value,
      '#element_validate' => array('\Drupal\focal_point\Plugin\Field\FieldWidget\FocalPointImageWidget::validateFocalPoint'),
      '#attributes' => array(
        'class' => array('focal-point', $element_selector),
        'data-selector' => $element_selector,
        'data-field-name' => $element['#field_name'],
      ),
      '#attached' => array(
        'library' => array('focal_point/drupal.focal_point'),
      ),
    );

    return $element;
  }
Ejemplo n.º 4
0
 /**
  * Form API callback: Processes a image_image field element.
  *
  * Expands the image_image type to include the alt and title fields.
  *
  * This method is assigned as a #process callback in formElement() method.
  */
 public static function process($element, FormStateInterface $form_state, $form)
 {
     $element = parent::process($element, $form_state, $form);
     $item = $element['#value'];
     $item['fids'] = $element['fids']['#value'];
     $element['#theme'] = 'imagefield_crop_widget';
     $element['#attached']['library'][] = 'imagefield_crop/core';
     $cropbox_id = Html::getUniqueId('cropbox-image');
     $element['#id'] = $cropbox_id;
     // Add the image preview.
     if (!empty($element['#files']) && $element['#preview_image_style']) {
         $element['#description'] = t('Click on the image and drag to mark how the image will be cropped');
         $file = reset($element['#files']);
         $variables = array('style_name' => $element['#preview_image_style'], 'uri' => $file->getFileUri());
         // Determine image dimensions.
         if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
             $variables['width'] = $element['#value']['width'];
             $variables['height'] = $element['#value']['height'];
         } else {
             $image = \Drupal::service('image.factory')->get($file->getFileUri());
             if ($image->isValid()) {
                 $variables['width'] = $image->getWidth();
                 $variables['height'] = $image->getHeight();
             } else {
                 $variables['width'] = $variables['height'] = NULL;
             }
         }
         if ($element['#imagefield_crop_show_preview']) {
             $element['preview']['#theme'] = 'imagefield_crop_preview';
         } else {
             $element['preview']['#access'] = FALSE;
         }
         $element['cropbox'] = array('#theme' => 'image', '#uri' => $file->getFileUri(), '#attributes' => array('class' => 'cropbox', 'id' => $cropbox_id . '-cropbox'), '#description' => t('Click on the image and drag to mark how the image will be cropped.'));
         $element['cropinfo'] = self::addCropInfoFields($file->get('fid')->getString());
         list($res_w, $res_h) = explode('x', $element['#imagefield_crop_resolution']);
         list($crop_w, $crop_h) = explode('x', $element['#imagefield_crop_croparea']);
         $settings = array($cropbox_id => array('box' => array('ratio' => $res_h ? $element['#imagefield_crop_enforce_ratio'] * $res_w / $res_h : 0, 'box_width' => $crop_w, 'box_height' => $crop_h), 'minimum' => array('width' => $element['#imagefield_crop_enforce_minimum'] ? $res_w : NULL, 'height' => $element['#imagefield_crop_enforce_minimum'] ? $res_h : NULL), 'preview' => $element['#imagefield_crop_show_preview'], 'preview_info' => array('orig_width' => $variables['width'], 'orig_height' => $variables['height'], 'width' => (int) $res_w, 'height' => (int) $res_h)));
         $element['#attached']['drupalSettings']['imagefield_crop'] = $settings;
     }
     return $element;
 }
Ejemplo n.º 5
0
 /**
  * Form API callback: Processes a crop_image field element.
  *
  * Expands the image_image type to include the alt and title fields.
  *
  * This method is assigned as a #process callback in formElement() method.
  */
 public static function process($element, FormStateInterface $form_state, $form)
 {
     $item = $element['#value'];
     $item['fids'] = $element['fids']['#value'];
     $edit = FALSE;
     $route_params = \Drupal::requestStack()->getCurrentRequest()->attributes->get('_route_params');
     if (isset($route_params['_entity_form']) && preg_match('/.edit/', $route_params['_entity_form'])) {
         $edit = TRUE;
     }
     $element['#theme'] = 'image_widget';
     $element['#attached']['library'][] = 'image/form';
     $element['#attached']['library'][] = 'image_widget_crop/drupal.image_widget_crop.admin';
     $element['#attached']['library'][] = 'image_widget_crop/drupal.image_widget_crop.upload.admin';
     // Add the image preview.
     if (!empty($element['#files']) && $element['#preview_image_style']) {
         $file = reset($element['#files']);
         $variables = array('style_name' => $element['#preview_image_style'], 'uri' => $file->getFileUri(), 'file_id' => $file->id());
         /** @var \Drupal\image_widget_crop\ImageWidgetCrop $ImageWidgetCrop */
         $ImageWidgetCrop = new ImageWidgetCrop();
         // Determine image dimensions.
         if (isset($element['#value']['width']) && isset($element['#value']['height'])) {
             $variables['width'] = $element['#value']['width'];
             $variables['height'] = $element['#value']['height'];
         } else {
             $image = \Drupal::service('image.factory')->get($file->getFileUri());
             if ($image->isValid()) {
                 $variables['width'] = $image->getWidth();
                 $variables['height'] = $image->getHeight();
             } else {
                 $variables['width'] = $variables['height'] = NULL;
             }
         }
         $element['crop_preview_wrapper'] = ['#type' => 'container', '#prefix' => '<ul>', '#suffix' => '</ul>', '#attributes' => ['class' => ['preview-wrapper-crop']], '#weight' => 100];
         $image_styles = \Drupal::service('entity.manager')->getStorage('image_style')->loadByProperties(['status' => TRUE]);
         if ($image_styles) {
             /** @var \Drupal\image\Entity\ImageStyle $image_style */
             foreach ($image_styles as $image_style) {
                 if (in_array($image_style->getName(), $element['#crop_list'])) {
                     // Get the ratio of image by ImageStyle.
                     $ratio = $ImageWidgetCrop->getSizeRatio($image_style);
                     // Generation of html List with image & crop informations.
                     // @todo Create new elements for styling the crop container & lists.
                     $element['crop_preview_wrapper'][$image_style->getName()] = ['#type' => 'container', '#prefix' => "<li data-ratio={$ratio}>", '#suffix' => '</li>', '#attributes' => ['class' => ['crop-preview-wrapper-list']], '#weight' => -10];
                     $element['crop_preview_wrapper'][$image_style->getName()]['title'] = ['#prefix' => '<p>', '#suffix' => '</p>', '#markup' => t('@style_label - ( <b>real ratio</b> @ratio )', ['@style_label' => $image_style->label(), '@ratio' => $ratio])];
                     $element['crop_preview_wrapper'][$image_style->getName()]['image'] = ['#theme' => 'image_style', '#style_name' => $element['#crop_preview_image_style'], '#uri' => $variables['uri']];
                     // GET CROP LIBRARIE VALUES.
                     $crop_elements = ['x1' => ['label' => t('crop x1'), 'value' => NULL], 'x2' => ['label' => t('crop x2'), 'value' => NULL], 'y1' => ['label' => t('crop y1'), 'value' => NULL], 'y2' => ['label' => t('crop y2'), 'value' => NULL], 'crop-w' => ['label' => t('crop size width'), 'value' => NULL], 'crop-h' => ['label' => t('crop size height'), 'value' => NULL], 'thumb-w' => ['label' => t('Thumbnail Width'), 'value' => NULL], 'thumb-h' => ['label' => t('Thumbnail Height'), 'value' => NULL]];
                     if ($edit) {
                         $crop = \Drupal::service('entity.manager')->getStorage('crop')->loadByProperties(['type' => $ImageWidgetCrop->getCropType($image_style), 'uri' => $variables['uri'], 'image_style' => $image_style->getName()]);
                         // Only if the crop already exist pre-populate,
                         // all cordinates values.
                         if (!empty($crop)) {
                             /** @var \Drupal\crop\Entity\Crop $crop_entity */
                             foreach ($crop as $crop_id => $crop_entity) {
                                 $crop_properties = ['anchor' => $crop_entity->position(), 'size' => $crop_entity->size()];
                             }
                             // If the current crop have a position & sizes,
                             // calculate properties to apply crop selection into preview.
                             if (isset($crop_properties)) {
                                 $values = static::getThumbnailCropProperties($variables['uri'], $crop_properties);
                             }
                             if (!empty($values)) {
                                 // Populate form crop value with values store into crop API.
                                 foreach ($crop_elements as $properties => $value) {
                                     $crop_elements[$properties]['value'] = $values[$properties];
                                 }
                             }
                         }
                     }
                     // Generate all cordinates elements into the form when,
                     // process is active.
                     foreach ($crop_elements as $crop_elements_name => $crop_elements_value) {
                         $element['crop_preview_wrapper'][$image_style->getName()][$crop_elements_name] = ['#type' => 'textfield', '#title' => $crop_elements_value['label'], '#attributes' => ['class' => ["crop-{$crop_elements_name}"]], '#default_value' => !empty($edit) ? $crop_elements_value['value'] : NULL];
                     }
                     // Stock Original File Values.
                     $element['file-uri'] = ['#type' => 'value', '#value' => $variables['uri']];
                     $element['file-id'] = ['#type' => 'value', '#value' => $variables['file_id']];
                 }
             }
         }
     }
     return parent::process($element, $form_state, $form);
 }