Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\custom_block\CustomBlockFormController.
*/
namespace Drupal\custom_block;
Angie Byron
committed
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityFormController;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
catch
committed
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Language\Language;
Angie Byron
committed
use Drupal\Core\Language\LanguageManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
Dries Buytaert
committed
/**
* Form controller for the custom block edit forms.
*/
class CustomBlockFormController extends ContentEntityFormController {
Dries Buytaert
committed
Angie Byron
committed
/**
* The custom block storage.
*
catch
committed
* @var \Drupal\Core\Entity\EntityStorageInterface
Angie Byron
committed
*/
protected $customBlockStorage;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManager
*/
protected $languageManager;
/**
* Constructs a CustomBlockFormController object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
catch
committed
* @param \Drupal\Core\Entity\EntityStorageInterface $custom_block_storage
* The custom block storage.
Angie Byron
committed
* @param \Drupal\Core\Language\LanguageManager $language_manager
* The language manager.
*/
catch
committed
public function __construct(EntityManagerInterface $entity_manager, EntityStorageInterface $custom_block_storage, LanguageManager $language_manager) {
Angie Byron
committed
parent::__construct($entity_manager);
$this->customBlockStorage = $custom_block_storage;
$this->languageManager = $language_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$entity_manager = $container->get('entity.manager');
return new static(
$entity_manager,
catch
committed
$entity_manager->getStorage('custom_block'),
Angie Byron
committed
$container->get('language_manager')
);
}
Dries Buytaert
committed
/**
* 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;
Dries Buytaert
committed
// Set up default values, if required.
$block_type = entity_load('custom_block_type', $block->bundle());
if (!$block->isNew()) {
$block->setRevisionLog(NULL);
Dries Buytaert
committed
}
// Always use the default revision setting.
$block->setNewRevision($block_type->revision);
}
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function form(array $form, array &$form_state) {
$block = $this->entity;
Angie Byron
committed
$account = $this->currentUser();
if ($this->operation == 'edit') {
$form['#title'] = $this->t('Edit custom block %label', array('%label' => $block->label()));
Dries Buytaert
committed
// 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->bundle() . '-form');
Dries Buytaert
committed
// Basic block information.
$form['info'] = array(
'#type' => 'textfield',
Angie Byron
committed
'#title' => $this->t('Block description'),
Dries Buytaert
committed
'#required' => TRUE,
'#default_value' => $block->label(),
Dries Buytaert
committed
'#weight' => -5,
Angie Byron
committed
'#description' => $this->t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => $this->url('block.admin_display'))),
Dries Buytaert
committed
);
Angie Byron
committed
if ($this->moduleHandler->moduleExists('language')) {
$language_configuration = language_get_default_configuration('custom_block', $block->bundle());
Dries Buytaert
committed
Angie Byron
committed
// Set the correct default language.
if ($block->isNew()) {
$language_default = $this->languageManager->getCurrentLanguage($language_configuration['langcode']);
Angie Byron
committed
$block->langcode->value = $language_default->id;
}
Dries Buytaert
committed
}
$form['langcode'] = array(
Angie Byron
committed
'#title' => $this->t('Language'),
Dries Buytaert
committed
'#type' => 'language_select',
'#default_value' => $block->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL,
Dries Buytaert
committed
'#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
);
$form['advanced'] = array(
Dries Buytaert
committed
'#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',
Angie Byron
committed
'#title' => $this->t('Revision information'),
Angie Byron
committed
// Open by default when "Create new revision" is checked.
'#open' => $block->isNewRevision(),
'#group' => 'advanced',
Dries Buytaert
committed
'#attributes' => array(
'class' => array('custom-block-form-revision-information'),
),
'#attached' => array(
'library' => array('custom_block/drupal.custom_block'),
Dries Buytaert
committed
),
'#weight' => 20,
Angie Byron
committed
'#access' => $block->isNewRevision() || $account->hasPermission('administer blocks'),
Dries Buytaert
committed
);
$form['revision_information']['revision'] = array(
'#type' => 'checkbox',
Angie Byron
committed
'#title' => $this->t('Create new revision'),
Dries Buytaert
committed
'#default_value' => $block->isNewRevision(),
Angie Byron
committed
'#access' => $account->hasPermission('administer blocks'),
Dries Buytaert
committed
);
// 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',
Angie Byron
committed
'#title' => $this->t('Revision log message'),
Dries Buytaert
committed
'#rows' => 4,
'#default_value' => $block->getRevisionLog(),
Angie Byron
committed
'#description' => $this->t('Briefly desribe the changes you have made.'),
Dries Buytaert
committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
);
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;
}
/**
Angie Byron
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function save(array $form, array &$form_state) {
$block = $this->entity;
$insert = $block->isNew();
Dries Buytaert
committed
$block->save();
$watchdog_args = array('@type' => $block->bundle(), '%info' => $block->label());
$block_type = entity_load('custom_block_type', $block->bundle());
Dries Buytaert
committed
$t_args = array('@type' => $block_type->label(), '%info' => $block->label());
if ($insert) {
watchdog('content', '@type: added %info.', $watchdog_args, WATCHDOG_NOTICE);
Angie Byron
committed
drupal_set_message($this->t('@type %info has been created.', $t_args));
Dries Buytaert
committed
}
else {
watchdog('content', '@type: updated %info.', $watchdog_args, WATCHDOG_NOTICE);
Angie Byron
committed
drupal_set_message($this->t('@type %info has been updated.', $t_args));
Dries Buytaert
committed
}
if ($block->id()) {
$form_state['values']['id'] = $block->id();
$form_state['id'] = $block->id();
Dries Buytaert
committed
if ($insert) {
Angie Byron
committed
if (!$theme = $block->getTheme()) {
$theme = $this->config('system.theme')->get('default');
Dries Buytaert
committed
}
Angie Byron
committed
$form_state['redirect_route'] = array(
'route_name' => 'block.admin_add',
'route_parameters' => array(
'plugin_id' => 'custom_block:' . $block->uuid(),
'theme' => $theme,
),
);
Dries Buytaert
committed
}
else {
Angie Byron
committed
$form_state['redirect_route']['route_name'] = 'custom_block.list';
Dries Buytaert
committed
}
}
else {
// In the unlikely case something went wrong on save, the block will be
// rebuilt and block form redisplayed.
Angie Byron
committed
drupal_set_message($this->t('The block could not be saved.'), 'error');
Dries Buytaert
committed
$form_state['rebuild'] = TRUE;
}
// Clear the page and block caches.
Angie Byron
committed
Cache::invalidateTags(array('content' => TRUE));
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, array &$form_state) {
if ($this->entity->isNew()) {
Angie Byron
committed
$exists = $this->customBlockStorage->loadByProperties(array('info' => $form_state['values']['info']));
if (!empty($exists)) {
$this->setFormError('info', $form_state, $this->t('A block with description %name already exists.', array(
Angie Byron
committed
'%name' => $form_state['values']['info'],
)));
}
}
}