CustomBlockTypeForm.php 3.35 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\custom_block\CustomBlockTypeForm.
6 7 8 9
 */

namespace Drupal\custom_block;

10
use Drupal\Core\Entity\EntityForm;
11
use Drupal\Core\Entity\EntityTypeInterface;
12 13

/**
14
 * Base form for category edit forms.
15
 */
16
class CustomBlockTypeForm extends EntityForm {
17 18

  /**
19
   * Overrides \Drupal\Core\Entity\EntityForm::form().
20
   */
21 22
  public function form(array $form, array &$form_state) {
    $form = parent::form($form, $form_state);
23

24
    $block_type = $this->entity;
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39
    $form['label'] = array(
      '#type' => 'textfield',
      '#title' => t('Label'),
      '#maxlength' => 255,
      '#default_value' => $block_type->label(),
      '#description' => t("Provide a label for this block type to help identify it in the administration pages."),
      '#required' => TRUE,
    );
    $form['id'] = array(
      '#type' => 'machine_name',
      '#default_value' => $block_type->id(),
      '#machine_name' => array(
        'exists' => 'custom_block_type_load',
      ),
40
      '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      '#disabled' => !$block_type->isNew(),
    );

    $form['description'] = array(
      '#type' => 'textarea',
      '#default_value' => $block_type->description,
      '#description' => t('Enter a description for this block type.'),
      '#title' => t('Description'),
    );

    $form['revision'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create new revision'),
      '#default_value' => $block_type->revision,
      '#description' => t('Create a new revision by default for this block type.')
    );

58
    if ($this->moduleHandler->moduleExists('content_translation')) {
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      $form['language'] = array(
        '#type' => 'details',
        '#title' => t('Language settings'),
        '#group' => 'additional_settings',
      );

      $language_configuration = language_get_default_configuration('custom_block', $block_type->id());
      $form['language']['language_configuration'] = array(
        '#type' => 'language_configuration',
        '#entity_information' => array(
          'entity_type' => 'custom_block',
          'bundle' => $block_type->id(),
        ),
        '#default_value' => $language_configuration,
      );

      $form['#submit'][] = 'language_configuration_element_submit';
    }

    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
    );

    return $form;
  }

  /**
88
   * Overrides \Drupal\Core\Entity\EntityForm::save().
89 90
   */
  public function save(array $form, array &$form_state) {
91
    $block_type = $this->entity;
92 93
    $status = $block_type->save();

94
    $edit_link = \Drupal::linkGenerator()->generateFromUrl($this->t('Edit'), $this->entity->urlInfo());
95 96
    if ($status == SAVED_UPDATED) {
      drupal_set_message(t('Custom block type %label has been updated.', array('%label' => $block_type->label())));
97
      watchdog('custom_block', 'Custom block type %label has been updated.', array('%label' => $block_type->label()), WATCHDOG_NOTICE, $edit_link);
98 99 100
    }
    else {
      drupal_set_message(t('Custom block type %label has been added.', array('%label' => $block_type->label())));
101
      watchdog('custom_block', 'Custom block type %label has been added.', array('%label' => $block_type->label()), WATCHDOG_NOTICE, $edit_link);
102 103
    }

104
    $form_state['redirect_route']['route_name'] = 'custom_block.type_list';
105 106 107
  }

}