You are here

function _crop_media_provider_form in Crop API 8

Helper function to avoid uneeded code duplication.

@todo Delete this and media entity fallback when media is stable.

2 calls to _crop_media_provider_form()
crop_form_media_bundle_edit_form_alter in ./crop.module
Implements hook_form_FORM_ID_alter().
crop_form_media_type_edit_form_alter in ./crop.module
Implements hook_form_FORM_ID_alter().

File

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

Code

function _crop_media_provider_form(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\Core\Config\Entity\ConfigEntityBundleBase $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();
    }
  }

  // Maintain compatibility with Media Entity.
  if ($entity_type instanceof MediaType) {
    $form['#entity_builders'][] = 'crop_media_type_form_builder';
  }
  else {
    $form['#entity_builders'][] = 'crop_media_bundle_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' => '_none',
    '#description' => t('Select field that stores image which needs to be cropped.'),
  ];
  return $form;
}