You are here

ViewsMegarowLinkEdit.php in Views Megarow 8

File

src/Plugin/views/field/ViewsMegarowLinkEdit.php
View source
<?php

namespace Drupal\views_megarow\Plugin\views\field;

use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\EntityLinkEdit;
use Drupal\views\ResultRow;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;

/**
 * Field handler to present a link node edit.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("views_megarow_node_link_edit")
 */
class ViewsMegarowLinkEdit extends EntityLinkEdit {

  /**
   * {@inheritdoc}
   */
  public function usesGroupBy() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function query() {

    // do nothing -- to override the parent query.
  }

  /**
   * {@inheritdoc}
   */
  protected function defineOptions() {
    $options = parent::defineOptions();

    // Override the alter text option to always alter the text.
    //    $options['alter']['contains']['alter_text'] = array('default' => TRUE);
    //    $options['hide_alter_empty'] = array('default' => FALSE);
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);

    // Remove the checkbox
    //    unset($form['alter']['alter_text']);
    //    unset($form['alter']['text']['#states']);
    //    unset($form['alter']['help']['#states']);
    //    $form['#pre_render'][] = array($this, 'preRenderCustomForm');
  }
  public function render(ResultRow $values) {
    $entity_type = $values->_entity
      ->getEntityType()
      ->id();
    $id = $values->_entity
      ->id();
    $options = [
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 600,
        ]),
      ],
    ];
    $content = [];
    $content['vm_modal_edition'] = [
      '#type' => 'link',
      '#title' => t('Edit'),
      '#url' => Url::fromRoute('views_megarow.edit', [
        'entity_type' => $entity_type,
        'entity' => $id,
        'js' => 'nojs',
      ]),
      '#options' => $options,
      '#attached' => [
        'library' => [
          'core/drupal.dialog.ajax',
        ],
      ],
    ];

    //$content['l']['#markup'] = \Drupal::l('coucou', new Url('entity.node.canonical', ['node' => 1], array('attributes' => array('class' => array('test-refresh')))));

    //    $content['l']['#attached']['library'][] = 'views_megarow/link';
    return $content;
  }

  /**
   * Prerender function to move the textarea to the top of a form.
   *
   * @param array $form
   *   The form build array.
   *
   * @return array
   *   The modified form build array.
   */
  public function preRenderCustomForm($form) {
    $form['text'] = $form['alter']['text'];
    $form['help'] = $form['alter']['help'];
    unset($form['alter']['text']);
    unset($form['alter']['help']);
    return $form;
  }

}

Classes

Namesort descending Description
ViewsMegarowLinkEdit Field handler to present a link node edit.