Newer
Older
<?php
/**
* @file
Alex Pott
committed
* Contains Drupal\responsive_image\ResponsiveImageForm.
namespace Drupal\responsive_image;
use Drupal\Component\Utility\String;
Alex Pott
committed
use Drupal\Core\Entity\EntityForm;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
* Form controller for the responsive image edit/add forms.
Alex Pott
committed
class ResponsiveImageMappingForm extends EntityForm {
Alex Pott
committed
* Overrides Drupal\Core\Entity\EntityForm::form().
*
* @param array $form
* A nested array form elements comprising the form.
Dries Buytaert
committed
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping
* The entity being edited.
*
* @return array
* The array containing the complete form.
*/
Dries Buytaert
committed
public function form(array $form, FormStateInterface $form_state) {
Alex Pott
committed
if ($this->operation == 'duplicate') {
$form['#title'] = $this->t('<em>Duplicate responsive image mapping</em> @label', array('@label' => $this->entity->label()));
Alex Pott
committed
$this->entity = $this->entity->createDuplicate();
}
if ($this->operation == 'edit') {
$form['#title'] = $this->t('<em>Edit responsive image mapping</em> @label', array('@label' => $this->entity->label()));
Alex Pott
committed
}
/** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
$responsive_image_mapping = $this->entity;
$form['label'] = array(
'#type' => 'textfield',
'#title' => $this->t('Label'),
'#maxlength' => 255,
'#default_value' => $responsive_image_mapping->label(),
'#description' => $this->t("Example: 'Hero image' or 'Author image'."),
'#required' => TRUE,
);
$form['id'] = array(
'#type' => 'machine_name',
'#default_value' => $responsive_image_mapping->id(),
'#machine_name' => array(
'exists' => '\Drupal\responsive_image\Entity\ResponsiveImageMapping::load',
'source' => array('label'),
),
'#disabled' => (bool) $responsive_image_mapping->id() && $this->operation != 'duplicate',
if ((bool) $responsive_image_mapping->id() && $this->operation != 'duplicate') {
$description = $this->t('Select a breakpoint group from the enabled themes.') . ' ' . $this->t("Warning: if you change the breakpoint group you lose all your selected mappings.");
$description = $this->t('Select a breakpoint group from the enabled themes.');
}
$form['breakpointGroup'] = array(
'#type' => 'select',
'#title' => $this->t('Breakpoint group'),
'#default_value' => ($responsive_image_mapping->getBreakpointGroup() != '') ? $responsive_image_mapping->getBreakpointGroup()->id() : '',
'#options' => breakpoint_group_select_options(),
'#required' => TRUE,
'#description' => $description,
);
$image_styles = image_style_options(TRUE);
Dries Buytaert
committed
$image_styles[RESPONSIVE_IMAGE_EMPTY_IMAGE] = $this->t('- empty image -');
foreach ($responsive_image_mapping->getMappings() as $breakpoint_id => $mapping) {
foreach ($mapping as $multiplier => $image_style) {
$breakpoint = $responsive_image_mapping->getBreakpointGroup()->getBreakpointById($breakpoint_id);
Angie Byron
committed
$label = $multiplier . ' ' . $breakpoint->name . ' [' . $breakpoint->mediaQuery . ']';
$form['mappings'][$breakpoint_id][$multiplier] = array(
'#type' => 'select',
'#title' => String::checkPlain($label),
'#options' => $image_styles,
'#default_value' => $image_style,
'#description' => $this->t('Select an image style for this breakpoint.'),
);
}
}
$form['#tree'] = TRUE;
return parent::form($form, $form_state, $responsive_image_mapping);
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
public function validate(array $form, FormStateInterface $form_state) {
/** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
$responsive_image_mapping = $this->entity;
// Only validate on edit.
Alex Pott
committed
if ($form_state->hasValue('mappings')) {
$responsive_image_mapping->setMappings($form_state->getValue('mappings'));
// Check if another breakpoint group is selected.
Alex Pott
committed
if ($form_state->getValue('breakpointGroup') != $form_state['complete_form']['breakpointGroup']['#default_value']) {
// Remove the mappings.
Alex Pott
committed
$form_state->unsetValue('mappings');
}
// Make sure at least one mapping is defined.
elseif (!$responsive_image_mapping->isNew() && !$responsive_image_mapping->hasMappings()) {
$form_state->setErrorByName('mappings', $this->t('Please select at least one mapping.'));
}
}
}
/**
Alex Pott
committed
* Overrides Drupal\Core\Entity\EntityForm::save().
Dries Buytaert
committed
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\responsive_image\ResponsiveImageMappingInterface $responsive_image_mapping */
$responsive_image_mapping = $this->entity;
$responsive_image_mapping->save();
$this->logger('responsive_image')->notice('Responsive image mapping @label saved.', array('@label' => $responsive_image_mapping->label()));
drupal_set_message($this->t('Responsive image mapping %label saved.', array('%label' => $responsive_image_mapping->label())));
// Redirect to edit form after creating a new mapping or after selecting
// another breakpoint group.
if (!$responsive_image_mapping->hasMappings()) {
Alex Pott
committed
$form_state->setRedirect(
'entity.responsive_image_mapping.edit_form',
array('responsive_image_mapping' => $responsive_image_mapping->id())
Angie Byron
committed
);
Alex Pott
committed
$form_state->setRedirect('responsive_image.mapping_page');