You are here

SearchAddForm.php in Entity Share 8.2

Same filename and directory in other branches
  1. 8.3 modules/entity_share_server/src/Form/SearchAddForm.php

File

modules/entity_share_server/src/Form/SearchAddForm.php
View source
<?php

declare (strict_types=1);
namespace Drupal\entity_share_server\Form;

use Drupal\Core\Form\FormStateInterface;

/**
 * Class SearchAddForm.
 *
 * @package Drupal\entity_share_server\Form
 */
class SearchAddForm extends SearchBaseForm {

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $form['path'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Path'),
      '#description' => $this
        ->t('Enter the machine name of the field / property you want to filter on. You can reference field / property of a referenced entity. Example: uid.name for the name of the author.'),
      '#required' => TRUE,
    ];
    $form['search_id'] = [
      '#type' => 'machine_name',
      '#title' => $this
        ->t('ID'),
      '#machine_name' => [
        'source' => [
          'path',
        ],
        'exists' => [
          $this,
          'searchExists',
        ],
      ],
    ];
    $form['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Label'),
      '#required' => TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(array $form, FormStateInterface $form_state) {

    /** @var \Drupal\entity_share_server\Entity\ChannelInterface $channel */
    $channel = $this->entity;
    $channel_searches = $channel
      ->get('channel_searches');
    if (is_null($channel_searches)) {
      $channel_searches = [];
    }
    $channel_searches[$form_state
      ->getValue('search_id')] = [
      'path' => $form_state
        ->getValue('path'),
      'label' => $form_state
        ->getValue('label'),
    ];
    $channel
      ->set('channel_searches', $channel_searches);
    $channel
      ->save();
    $form_state
      ->setRedirectUrl($channel
      ->toUrl('edit-form'));
  }

}

Classes

Namesort descending Description
SearchAddForm Class SearchAddForm.