You are here

function filefield_paths_form_field_config_edit_form_alter in File (Field) Paths 8

Implements hook_form_FORM_ID_alter().

File

./filefield_paths.module, line 39
Contains core functions for the File (Field) Paths module.

Code

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

  /** @var Drupal\field\Entity\FieldConfig $field */
  $field = $form_state
    ->getFormObject()
    ->getEntity();
  $class = $field
    ->getClass();
  if (class_exists($class) && new $class($field
    ->getItemDefinition()) instanceof FileFieldItemList) {
    $entity_info = \Drupal::entityTypeManager()
      ->getDefinition($field
      ->getTargetEntityTypeId());
    $settings = $field
      ->getThirdPartySettings('filefield_paths');
    $form['settings']['filefield_paths'] = [
      '#type' => 'container',
      '#tree' => TRUE,
      '#weight' => 2,
      '#parents' => [
        'third_party_settings',
        'filefield_paths',
      ],
    ];
    $form['settings']['filefield_paths']['enabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Enable File (Field) Paths?'),
      '#default_value' => isset($settings['enabled']) ? $settings['enabled'] : TRUE,
      '#description' => t('File (Field) Paths provides advanced file path and naming options.'),
    ];

    // Hide standard File directory field.
    $form['settings']['file_directory']['#states'] = [
      'visible' => [
        ':input[name="third_party_settings[filefield_paths][enabled]"]' => [
          'checked' => FALSE,
        ],
      ],
    ];

    // File (Field) Paths details element.
    $form['settings']['filefield_paths']['details'] = [
      '#type' => 'details',
      '#title' => t('File (Field) Path settings'),
      '#weight' => 3,
      '#tree' => TRUE,
      '#states' => [
        'visible' => [
          ':input[name="third_party_settings[filefield_paths][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#parents' => [
        'third_party_settings',
        'filefield_paths',
      ],
    ];

    // Additional File (Field) Paths widget fields.
    $settings_fields = \Drupal::moduleHandler()
      ->invokeAll('filefield_paths_field_settings', [
      $form,
    ]);
    foreach ($settings_fields as $name => $settings_field) {

      // Attach widget fields.
      $form['settings']['filefield_paths']['details'][$name] = [
        '#type' => 'container',
      ];

      // Attach widget field form elements.
      if (isset($settings_field['form']) && is_array($settings_field['form'])) {
        foreach (array_keys($settings_field['form']) as $delta => $key) {
          $form['settings']['filefield_paths']['details'][$name][$key] = $settings_field['form'][$key];
          if (\Drupal::moduleHandler()
            ->moduleExists('token')) {
            $form['settings']['filefield_paths']['details'][$name][$key]['#element_validate'][] = 'token_element_validate';
            $form['settings']['filefield_paths']['details'][$name][$key]['#token_types'] = [
              'date',
              'file',
            ];
            $token_type = \Drupal::service('token.entity_mapper')
              ->getTokenTypeForEntityType($entity_info
              ->id());
            if (!empty($token_type)) {
              $form['settings']['filefield_paths']['details'][$name][$key]['#token_types'][] = $token_type;
            }
          }

          // Fetch stored value from instance.
          if (isset($settings[$name][$key])) {
            $form['settings']['filefield_paths']['details'][$name][$key]['#default_value'] = $settings[$name][$key];
          }
        }

        // Field options.
        $form['settings']['filefield_paths']['details'][$name]['options'] = [
          '#type' => 'details',
          '#title' => t('@title options', [
            '@title' => $settings_field['title'],
          ]),
          '#weight' => 1,
          '#attributes' => [
            'class' => [
              "{$name} cleanup",
            ],
          ],
        ];

        // Cleanup slashes (/).
        $form['settings']['filefield_paths']['details'][$name]['options']['slashes'] = [
          '#type' => 'checkbox',
          '#title' => t('Remove slashes (/) from tokens'),
          '#default_value' => isset($settings[$name]['options']['slashes']) ? $settings[$name]['options']['slashes'] : FALSE,
          '#description' => t('If checked, any slashes (/) in tokens will be removed from %title.', [
            '%title' => $settings_field['title'],
          ]),
        ];

        // Cleanup field with Pathauto module.
        $form['settings']['filefield_paths']['details'][$name]['options']['pathauto'] = [
          '#type' => 'checkbox',
          '#title' => t('Cleanup using Pathauto'),
          '#default_value' => isset($settings[$name]['options']['pathauto']) && \Drupal::moduleHandler()
            ->moduleExists('pathauto') ? $settings[$name]['options']['pathauto'] : FALSE,
          '#description' => t('Cleanup %title using Pathauto.', [
            '%title' => $settings_field['title'],
          ]),
          '#disabled' => TRUE,
        ];
        if (\Drupal::moduleHandler()
          ->moduleExists('pathauto')) {
          unset($form['settings']['filefield_paths']['details'][$name]['options']['pathauto']['#disabled']);
          $form['settings']['filefield_paths']['details'][$name]['options']['pathauto']['#description'] = t('Cleanup %title using <a href="@pathauto">Pathauto settings</a>.', [
            '%title' => $settings_field['title'],
            '@pathauto' => Url::fromRoute('pathauto.settings.form')
              ->toString(),
          ]);
        }

        // Transliterate field.
        $form['settings']['filefield_paths']['details'][$name]['options']['transliterate'] = [
          '#type' => 'checkbox',
          '#title' => t('Transliterate'),
          '#default_value' => isset($settings[$name]['options']['transliterate']) ? $settings[$name]['options']['transliterate'] : 0,
          '#description' => t('Provides one-way string transliteration (romanization) and cleans the %title during upload by replacing unwanted characters.', [
            '%title' => $settings_field['title'],
          ]),
        ];

        // Replacement patterns for field.
        if (\Drupal::moduleHandler()
          ->moduleExists('token')) {
          $form['settings']['filefield_paths']['details']['token_tree'] = [
            '#theme' => 'token_tree_link',
            '#token_types' => [
              'file',
            ],
            '#weight' => 10,
          ];
          if (!empty($token_type)) {
            $form['settings']['filefield_paths']['details']['token_tree']['#token_types'][] = $token_type;
          }
        }

        // Redirect.
        $form['settings']['filefield_paths']['details']['redirect'] = [
          '#type' => 'checkbox',
          '#title' => t('Create Redirect'),
          '#description' => t('Create a redirect to the new location when a previously uploaded file is moved.'),
          '#default_value' => isset($settings['redirect']) ? $settings['redirect'] : FALSE,
          '#weight' => 11,
        ];
        if (!\Drupal::moduleHandler()
          ->moduleExists('redirect')) {
          $form['settings']['filefield_paths']['details']['redirect']['#disabled'] = TRUE;
          $form['settings']['filefield_paths']['details']['redirect']['#description'] .= '<br />' . t('Requires the <a href="https://drupal.org/project/redirect" target="_blank">Redirect</a> module.');
        }

        // Retroactive updates.
        $form['settings']['filefield_paths']['details']['retroactive_update'] = [
          '#type' => 'checkbox',
          '#title' => t('Retroactive update'),
          '#description' => t('Move and rename previously uploaded files.') . '<div>' . t('<strong class="warning">Warning:</strong> This feature should only be used on developmental servers or with extreme caution.') . '</div>',
          '#weight' => 12,
        ];

        // Active updating.
        $form['settings']['filefield_paths']['details']['active_updating'] = [
          '#type' => 'checkbox',
          '#title' => t('Active updating'),
          '#default_value' => isset($settings['active_updating']) ? $settings['active_updating'] : FALSE,
          '#description' => t('Actively move and rename previously uploaded files as required.') . '<div>' . t('<strong class="warning">Warning:</strong> This feature should only be used on developmental servers or with extreme caution.') . '</div>',
          '#weight' => 13,
        ];
      }
    }
    $form['actions']['submit']['#submit'][] = 'filefield_paths_form_submit';
  }
}