You are here

public function BlockUploadBlock::blockForm in Block Upload 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/BlockUploadBlock.php, line 75

Class

BlockUploadBlock
Provides a Custom block.

Namespace

Drupal\block_upload\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $block_id = $this
    ->getDerivativeId();
  $form = parent::blockForm($form, $form_state);

  // Retrieve existing configuration for this block.
  $config = $this
    ->getConfiguration();

  // Add a form field to the existing block configuration form.
  $fields = BlockUploadManager::blockUploadGetFieldList();
  $form['block_upload_' . $block_id . '_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field'),
    '#description' => $this
      ->t('Select field you wish to upload file.'),
    '#options' => $fields,
    '#default_value' => \Drupal::state()
      ->get('block_upload_' . $block_id . '_field') ?: '',
    '#ajax' => [
      'callback' => [
        $this,
        'blockUploadAjaxCallback',
      ],
      'wrapper' => 'config',
      'effect' => 'fade',
    ],
  ];
  $form['block_upload_id'] = [
    '#type' => 'textfield',
    '#default_value' => $block_id,
    '#access' => FALSE,
  ];

  // Add field additional display options.
  $field_name = \Drupal::state()
    ->get('block_upload_' . $block_id . '_field') ?: '';
  $field = FieldStorageConfig::loadByName(explode('.', $field_name)[0], explode('.', $field_name)[1]);
  if (!empty($field)) {
    BlockUploadManager::blockUploadFieldOptionsFormElements($form, $block_id, $field
      ->getType());
  }
  return $form;
}