You are here

class MaestroTemplateBuilderCanvas in Maestro 3.x

Same name and namespace in other branches
  1. 8.2 modules/maestro_template_builder/src/Form/MaestroTemplateBuilderCanvas.php \Drupal\maestro_template_builder\Form\MaestroTemplateBuilderCanvas

Hierarchy

Expanded class hierarchy of MaestroTemplateBuilderCanvas

1 string reference to 'MaestroTemplateBuilderCanvas'
maestro_template_builder.routing.yml in modules/maestro_template_builder/maestro_template_builder.routing.yml
modules/maestro_template_builder/maestro_template_builder.routing.yml

File

modules/maestro_template_builder/src/Form/MaestroTemplateBuilderCanvas.php, line 16

Namespace

Drupal\maestro_template_builder\Form
View source
class MaestroTemplateBuilderCanvas extends FormBase {

  /**
   *
   */
  public function getFormId() {
    return 'template_canvas';
  }

  /**
   *
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // Everything in the base form is mandatory.  nothing really to check here.
  }

  /**
   *
   */
  public function cancelForm(array &$form, FormStateInterface $form_state) {

    // We cancel the modal dialog by first sending down the form's error state as the cancel is a submit.
    // we then close the modal.
    $response = new AjaxResponse();
    $form['status_messages'] = [
      '#type' => 'status_messages',
      '#weight' => -10,
    ];
    $response
      ->addCommand(new HtmlCommand('#template-canvas', $form));
    $response
      ->addCommand(new CloseModalDialogCommand());
    return $response;
  }

  /**
   *
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Do we have any errors?  if so, handle them by returning the form's HTML and replacing the form.
    if ($form_state
      ->getErrors()) {
      unset($form['#prefix'], $form['#suffix']);
      $form['status_messages'] = [
        '#type' => 'status_messages',
        '#weight' => -10,
      ];
      $response = new AjaxResponse();

      // Replaces the form HTML with the validated HTML.
      $response
        ->addCommand(new HtmlCommand('#template-canvas', $form));
      return $response;
    }
    else {
      $templateMachineName = $form_state
        ->getValue('template_machine_name');
      $height = $form_state
        ->getValue('canvas_height');
      $width = $form_state
        ->getValue('canvas_width');

      // Create the new task entry in the template.
      $template = MaestroEngine::getTemplate($templateMachineName);
      $template->canvas_height = $height;
      $template->canvas_width = $width;
      $template
        ->save();
      $response = new AjaxResponse();
      $response
        ->addCommand(new FireJavascriptCommand('alterCanvas', [
        'height' => $height,
        'width' => $width,
      ]));
      $response
        ->addCommand(new CloseModalDialogCommand());
      return $response;
    }
  }

  /**
   * Ajax callback for add-new-form button click.
   */
  public function buildForm(array $form, FormStateInterface $form_state, $templateMachineName = '') {
    $template = MaestroEngine::getTemplate($templateMachineName);

    // Need to validate this template to ensure that it exists.
    if ($template == NULL) {
      $form = [
        '#title' => $this
          ->t('Error!'),
        '#markup' => $this
          ->t("The template you are attempting to add a task to doesn't exist"),
      ];
      return $form;
    }
    $form = [
      '#title' => $this
        ->t('Change canvas size'),
      '#markup' => '<div id="maestro-template-error" class="messages messages--error"></div>',
    ];
    $form['#prefix'] = '<div id="template-canvas">';
    $form['#suffix'] = '</div>';
    $form['template_machine_name'] = [
      '#type' => 'hidden',
      '#title' => $this
        ->t('machine name of the template'),
      '#default_value' => $templateMachineName,
      '#required' => TRUE,
    ];
    $form['canvas_height'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Height of the canvas in pixels'),
      '#default_value' => $template->canvas_height,
      '#size' => 10,
      '#required' => TRUE,
    ];
    $form['canvas_width'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Width of the canvas in pixels'),
      '#default_value' => $template->canvas_width,
      '#size' => 10,
      '#required' => TRUE,
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['update'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Update Canvas'),
      '#required' => TRUE,
      '#submit' => [],
      '#ajax' => [
        'callback' => '::submitForm',
        'event' => 'click',
      ],
    ];
    $form['actions']['cancel'] = [
      '#type' => 'button',
      '#value' => $this
        ->t('Cancel'),
      '#required' => TRUE,
      '#ajax' => [
        'callback' => [
          $this,
          'cancelForm',
        ],
        'wrapper' => '',
      ],
    ];
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MaestroTemplateBuilderCanvas::buildForm public function Ajax callback for add-new-form button click. Overrides FormInterface::buildForm
MaestroTemplateBuilderCanvas::cancelForm public function
MaestroTemplateBuilderCanvas::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
MaestroTemplateBuilderCanvas::submitForm public function Form submission handler. Overrides FormInterface::submitForm
MaestroTemplateBuilderCanvas::validateForm public function Form validation handler. Overrides FormBase::validateForm
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.