You are here

function crop_form_media_type_edit_form_alter in Crop API 8.2

Same name and namespace in other branches
  1. 8 crop.module \crop_form_media_type_edit_form_alter()

Implements hook_form_FORM_ID_alter().

Adds crop configuration fields to media form.

File

./crop.module, line 89
The Crop API Drupal module.

Code

function crop_form_media_type_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\media\Entity\MediaType $entity_type */
  $entity_type = $form_state
    ->getFormObject()
    ->getEntity();
  $options = [];
  $allowed_field_types = [
    'file',
    'image',
  ];

  /** @var \Drupal\Core\Field\FieldDefinitionInterface[] $fields */
  $fields = \Drupal::service('entity_field.manager')
    ->getFieldDefinitions('media', $entity_type
    ->id());
  foreach ($fields as $field_name => $field) {
    if (in_array($field
      ->getType(), $allowed_field_types) && !$field
      ->getFieldStorageDefinition()
      ->isBaseField()) {
      $options[$field_name] = $field
        ->getLabel();
    }
  }
  $form['#entity_builders'][] = 'crop_media_type_form_builder';
  $form['crop'] = [
    '#type' => 'fieldset',
    '#title' => t('Crop configuration'),
    '#group' => 'source_dependent',
  ];
  if (empty($options)) {
    $form['crop']['image_field'] = [
      '#type' => 'value',
      '#value' => NULL,
    ];
    $form['crop']['message'] = [
      '#markup' => t('There are no file or image fields on this bundle at the moment. In order to configure crop add at least one such field and come back.'),
    ];
    return;
  }
  $form['crop']['image_field'] = [
    '#type' => 'select',
    '#title' => t('Image field'),
    '#default_value' => $entity_type
      ->getThirdPartySetting('crop', 'image_field'),
    '#options' => $options,
    '#empty_option' => t('- Skip field -'),
    '#empty_value' => MediaSourceInterface::METADATA_FIELD_EMPTY,
    '#description' => t('Select field that stores image which needs to be cropped.'),
  ];
}