class CustomBlockFormController

Form controller for the custom block edit forms.

Hierarchy

Expanded class hierarchy of CustomBlockFormController

File

drupal/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockFormController.php, line 17
Contains \Drupal\custom_block\CustomBlockFormController.

Namespace

Drupal\custom_block
View source
class CustomBlockFormController extends EntityFormControllerNG {

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::prepareEntity().
   *
   * Prepares the custom block object.
   *
   * Fills in a few default values, and then invokes hook_custom_block_prepare()
   * on all modules.
   */
  protected function prepareEntity() {
    $block = $this->entity;

    // Set up default values, if required.
    $block_type = entity_load('custom_block_type', $block->type->value);

    // If this is a new custom block, fill in the default values.
    if (isset($block->id->value)) {
      $block
        ->set('log', NULL);
    }

    // Always use the default revision setting.
    $block
      ->setNewRevision($block_type->revision);
    module_invoke_all('custom_block_prepare', $block);
  }

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::form().
   */
  public function form(array $form, array &$form_state) {
    $block = $this->entity;

    // Override the default CSS class name, since the user-defined custom block
    // type name in 'TYPE-block-form' potentially clashes with third-party class
    // names.
    $form['#attributes']['class'][0] = drupal_html_class('block-' . $block->type->value . '-form');

    // Basic block information.
    // These elements are just values so they are not even sent to the client.
    foreach (array(
      'revision_id',
      'id',
    ) as $key) {
      $form[$key] = array(
        '#type' => 'value',
        '#value' => $block->{$key}->value,
      );
    }
    $form['info'] = array(
      '#type' => 'textfield',
      '#title' => t('Block description'),
      '#required' => TRUE,
      '#default_value' => $block->info->value,
      '#weight' => -5,
      '#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array(
        '@overview' => url('admin/structure/block'),
      )),
    );
    $language_configuration = module_invoke('language', 'get_default_configuration', 'custom_block', $block->type->value);

    // Set the correct default language.
    if ($block
      ->isNew() && !empty($language_configuration['langcode'])) {
      $language_default = language($language_configuration['langcode']);
      $block->langcode->value = $language_default->langcode;
    }
    $form['langcode'] = array(
      '#title' => t('Language'),
      '#type' => 'language_select',
      '#default_value' => $block->langcode->value,
      '#languages' => Language::STATE_ALL,
      '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
    );
    $form['advanced'] = array(
      '#type' => 'vertical_tabs',
      '#weight' => 99,
    );

    // Add a log field if the "Create new revision" option is checked, or if the
    // current user has the ability to check that option.
    $form['revision_information'] = array(
      '#type' => 'details',
      '#title' => t('Revision information'),
      '#collapsible' => TRUE,
      // Collapsed by default when "Create new revision" is unchecked.
      '#collapsed' => !$block
        ->isNewRevision(),
      '#group' => 'advanced',
      '#attributes' => array(
        'class' => array(
          'custom-block-form-revision-information',
        ),
      ),
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'custom_block') . '/custom_block.js',
        ),
      ),
      '#weight' => 20,
      '#access' => $block
        ->isNewRevision() || user_access('administer blocks'),
    );
    $form['revision_information']['revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
      '#default_value' => $block
        ->isNewRevision(),
      '#access' => user_access('administer blocks'),
    );

    // Check the revision log checkbox when the log textarea is filled in.
    // This must not happen if "Create new revision" is enabled by default,
    // since the state would auto-disable the checkbox otherwise.
    if (!$block
      ->isNewRevision()) {
      $form['revision_information']['revision']['#states'] = array(
        'checked' => array(
          'textarea[name="log"]' => array(
            'empty' => FALSE,
          ),
        ),
      );
    }
    $form['revision_information']['log'] = array(
      '#type' => 'textarea',
      '#title' => t('Revision log message'),
      '#rows' => 4,
      '#default_value' => $block->log->value,
      '#description' => t('Briefly describe the changes you have made.'),
    );
    return parent::form($form, $form_state, $block);
  }

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::submit().
   *
   * Updates the custom block object by processing the submitted values.
   *
   * This function can be called by a "Next" button of a wizard to update the
   * form state's entity with the current step's values before proceeding to the
   * next step.
   */
  public function submit(array $form, array &$form_state) {

    // Build the block object from the submitted values.
    $block = parent::submit($form, $form_state);

    // Save as a new revision if requested to do so.
    if (!empty($form_state['values']['revision'])) {
      $block
        ->setNewRevision();
    }
    return $block;
  }

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::save().
   */
  public function save(array $form, array &$form_state) {
    $block = $this->entity;
    $insert = empty($block->id->value);
    $block
      ->save();
    $watchdog_args = array(
      '@type' => $block
        ->bundle(),
      '%info' => $block
        ->label(),
    );
    $block_type = entity_load('custom_block_type', $block->type->value);
    $t_args = array(
      '@type' => $block_type
        ->label(),
      '%info' => $block
        ->label(),
    );
    if ($insert) {
      watchdog('content', '@type: added %info.', $watchdog_args, WATCHDOG_NOTICE);
      drupal_set_message(t('@type %info has been created.', $t_args));
    }
    else {
      watchdog('content', '@type: updated %info.', $watchdog_args, WATCHDOG_NOTICE);
      drupal_set_message(t('@type %info has been updated.', $t_args));
    }
    if ($block->id->value) {
      $form_state['values']['id'] = $block->id->value;
      $form_state['id'] = $block->id->value;
      if ($insert) {
        if ($theme = $block
          ->getTheme()) {
          $form_state['redirect'] = 'admin/structure/block/add/custom_block:' . $block->uuid->value . '/' . $theme;
        }
        else {
          $form_state['redirect'] = 'admin/structure/block/add/custom_block:' . $block->uuid->value . '/' . config('system.theme')
            ->get('default');
        }
      }
      else {
        $form_state['redirect'] = 'admin/structure/block';
      }
    }
    else {

      // In the unlikely case something went wrong on save, the block will be
      // rebuilt and block form redisplayed.
      drupal_set_message(t('The block could not be saved.'), 'error');
      $form_state['rebuild'] = TRUE;
    }

    // Clear the page and block caches.
    cache_invalidate_tags(array(
      'content' => TRUE,
    ));
  }

  /**
   * Overrides \Drupal\Core\Entity\EntityFormController::delete().
   */
  public function delete(array $form, array &$form_state) {
    $destination = array();
    $query = \Drupal::request()->query;
    if (!is_null($query
      ->get('destination'))) {
      $destination = drupal_get_destination();
      $query
        ->remove('destination');
    }
    $block = $this
      ->buildEntity($form, $form_state);
    $form_state['redirect'] = array(
      'block/' . $block
        ->id() . '/delete',
      array(
        'query' => $destination,
      ),
    );
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomBlockFormController::delete public function Overrides \Drupal\Core\Entity\EntityFormController::delete(). Overrides EntityFormController::delete
CustomBlockFormController::form public function Overrides \Drupal\Core\Entity\EntityFormController::form(). Overrides EntityFormControllerNG::form
CustomBlockFormController::prepareEntity protected function Overrides \Drupal\Core\Entity\EntityFormController::prepareEntity(). Overrides EntityFormController::prepareEntity
CustomBlockFormController::save public function Overrides \Drupal\Core\Entity\EntityFormController::save(). Overrides EntityFormController::save
CustomBlockFormController::submit public function Overrides \Drupal\Core\Entity\EntityFormController::submit(). Overrides EntityFormController::submit
EntityFormController::$entity protected property The entity being used by this form.
EntityFormController::$operation protected property The name of the current operation.
EntityFormController::actions protected function Returns an array of supported actions for the current entity form. 17
EntityFormController::actionsElement protected function Returns the action form element for the current entity form.
EntityFormController::buildForm public function Form constructor. Overrides FormInterface::buildForm 1
EntityFormController::getBaseFormID public function Returns a string identifying the base form. Overrides BaseFormIdInterface::getBaseFormID
EntityFormController::getEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getEntity(). Overrides EntityFormControllerInterface::getEntity
EntityFormController::getFormDisplay public function Returns the form display. Overrides EntityFormControllerInterface::getFormDisplay
EntityFormController::getFormID public function Returns a unique string identifying the form. Overrides FormInterface::getFormID
EntityFormController::getFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getFormLangcode(). Overrides EntityFormControllerInterface::getFormLangcode
EntityFormController::getOperation public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::getOperation(). Overrides EntityFormControllerInterface::getOperation
EntityFormController::init protected function Initialize the form state and the entity before the first form build. 1
EntityFormController::isDefaultFormLangcode public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::isDefaultFormLangcode(). Overrides EntityFormControllerInterface::isDefaultFormLangcode
EntityFormController::setEntity public function Implements \Drupal\Core\Entity\EntityFormControllerInterface::setEntity(). Overrides EntityFormControllerInterface::setEntity
EntityFormController::setFormDisplay public function Sets the form display. Overrides EntityFormControllerInterface::setFormDisplay
EntityFormController::setOperation public function Sets the operation for this form.
EntityFormController::submitForm public function Form submission handler. Overrides FormInterface::submitForm
EntityFormController::updateFormLangcode protected function Updates the form language to reflect any change to the entity language.
EntityFormController::validateForm public function Form validation handler. Overrides FormInterface::validateForm
EntityFormController::__construct public function Constructs an EntityFormController object. 5
EntityFormControllerNG::buildEntity public function Overrides EntityFormController::buildEntity(). Overrides EntityFormController::buildEntity 2
EntityFormControllerNG::submitEntityLanguage protected function Overrides EntityFormController::submitEntityLanguage(). Overrides EntityFormController::submitEntityLanguage
EntityFormControllerNG::validate public function Overrides EntityFormController::validate(). Overrides EntityFormController::validate 3