You are here

class AnimateEditForm in Animate Any 8

Provides a edit form for edit/update the animation data from Animation list.

Hierarchy

Expanded class hierarchy of AnimateEditForm

1 string reference to 'AnimateEditForm'
animate_any.routing.yml in ./animate_any.routing.yml
animate_any.routing.yml

File

src/Form/AnimateEditForm.php, line 11

Namespace

Drupal\animate_any\Form
View source
class AnimateEditForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'animate_edit_form';
  }
  public function buildForm(array $form, FormStateInterface $form_state, $element = NULL) {
    $fetch = \Drupal::database()
      ->select("animate_any_settings", "a");
    $fetch
      ->fields('a');
    $fetch
      ->condition('a.aid', $element);
    $fetch_results = $fetch
      ->execute()
      ->fetchAssoc();
    $form = [];
    $form['#attached']['library'][] = 'animate_any/animate';
    $form['#tree'] = TRUE;
    $form['parent_class'] = [
      '#title' => 'Add Parent Class',
      '#description' => $this
        ->t('You can add body class like <em>body.front (for front page)</em> OR class with dot(.) prefix and Id with hash(#) prefix.'),
      '#type' => 'textfield',
      '#default_value' => $fetch_results['parent'],
    ];
    $form['aid'] = [
      '#type' => 'hidden',
      '#default_value' => $element,
    ];
    $form['animate_fieldset'] = [
      '#prefix' => '<div id="item-fieldset-wrapper">',
      '#suffix' => '</div>',
      '#tree' => TRUE,
      '#theme' => 'table',
      '#header' => [],
      '#rows' => [],
      '#attributes' => [
        'class' => 'animation',
      ],
    ];

    // Json decode to get json to array.
    $data = json_decode($fetch_results['identifier']);
    foreach ($data as $key => $value) {
      $section_identity = [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Section identity'),
        '#default_value' => $value->section_identity,
        '#description' => $this
          ->t("Add class with dot(.) prefix and Id with hash(#) prefix."),
      ];
      $section_event = [
        '#title' => $this
          ->t('Select event'),
        '#type' => 'select',
        '#options' => animate_on_event(),
        '#attributes' => [
          'class' => [
            'select_event',
          ],
        ],
        '#default_value' => $value->section_event,
      ];
      $section_animation = [
        '#type' => 'select',
        '#options' => animate_any_options(),
        '#title' => $this
          ->t('Section Animation'),
        '#default_value' => $value->section_animation,
        '#attributes' => [
          'class' => [
            'select_animate',
          ],
        ],
      ];
      $animation = [
        '#markup' => 'ANIMATE ANY',
        '#prefix' => '<h1 id="animate" class="" style="font-size: 30px;">',
        '#suffix' => '</h1>',
      ];
      $form['animate_fieldset'][$key] = [
        'section_identity' => &$section_identity,
        'section_event' => &$section_event,
        'section_animation' => &$section_animation,
        'animation' => &$animation,
      ];
      $form['animate_fieldset']['#rows'][$key] = [
        [
          'data' => &$section_identity,
        ],
        [
          'data' => &$section_event,
        ],
        [
          'data' => &$section_animation,
        ],
        [
          'data' => &$animation,
        ],
      ];
      unset($section_identity);
      unset($section_event);
      unset($section_animation);
      unset($animation);
    }
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Update Settings'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $op = (string) $form_state
      ->getValue('op');
    if ($op == $this
      ->t('Update Settings')) {
      $parent = $form_state
        ->getValue('parent_class');
      if (empty($parent)) {
        $form_state
          ->setErrorByName("parent_class", $this
          ->t("Please select parent class"));
      }
      foreach ($form_state
        ->getValue('animate_fieldset') as $key => $value) {
        if (empty($value['section_identity'])) {
          $form_state
            ->setErrorByName("animate_fieldset][{$key}][section_identity", $this
            ->t("Please select section identity for row @key", [
            '@key' => $key,
          ]));
        }
        if ($value['section_event'] == 'none') {
          $form_state
            ->setRebuild();
          $form_state
            ->setErrorByName("animate_fieldset][{$key}][section_event", $this
            ->t("Please select section event for row @key", [
            '@key' => $key,
          ]));
        }
        if ($value['section_animation'] == 'none') {
          $form_state
            ->setErrorByName("animate_fieldset][{$key}][section_animation", $this
            ->t("Please select section animation for row @key", [
            '@key' => $key,
          ]));
        }
      }
    }
  }

  /**
   * Submit for animate_any_settings.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Update the data for current element.
    $parent = $form_state
      ->getvalue('parent_class');
    $aid = $form_state
      ->getvalue('aid');
    $identifiers = json_encode($form_state
      ->getvalue('animate_fieldset'));
    $data = \Drupal::database()
      ->update('animate_any_settings');
    $data
      ->fields([
      'parent' => $parent,
      'identifier' => $identifiers,
    ]);
    $data
      ->condition('aid', $aid);
    $valid = $data
      ->execute();
    if ($valid) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Animation settings updated.'));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AnimateEditForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
AnimateEditForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AnimateEditForm::submitForm public function Submit for animate_any_settings. Overrides FormInterface::submitForm
AnimateEditForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
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. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
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. Overrides UrlGeneratorTrait::redirect
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.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
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.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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. 1
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.