You are here

function fixed_block_content_form_alter in Fixed Block Content 8

Implements hook_form_alter().

File

./fixed_block_content.module, line 38
Provides permanent custom content blocks.

Code

function fixed_block_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add an option to the custom block form to update the default content in
  // its fixed block.
  if (preg_match('/^block_content_.+_edit_form$/', $form_id) && \Drupal::currentUser()
    ->hasPermission('administer blocks') && ($form_object = $form_state
    ->getFormObject()) && $form_object instanceof EntityFormInterface && ($block_content = $form_object
    ->getEntity()) && $block_content instanceof BlockContentInterface) {

    /** @var \Drupal\fixed_block_content\FixedToContentMappingHandlerInterface $mapping_handler */
    $mapping_handler = \Drupal::entityTypeManager()
      ->getHandler('fixed_block_content', 'mapping_handler');

    // Search the fixed block of the edited custom block.
    if ($fixed_block = $mapping_handler
      ->getFixedBlock($block_content)) {
      $form['fixed_block_content_update'] = [
        '#type' => 'checkbox',
        '#title' => t('Update default content in the fixed block'),
        '#default_value' => FALSE,
        '#weight' => 99,
      ];
      array_unshift($form['actions']['submit']['#submit'], '_fixed_block_content_update_on_save');
      $form_state
        ->setTemporaryValue('fixed_block_content_fbid', $fixed_block
        ->id());
    }
  }
}