未发布的节点

问题描述

以表格形式列出Views已发布的节点。添加到带有将包含链接的列的表中。通过单击链接,应该发送ajax请求,这将使该节点未发布。

我有视图,有链接,有警报-ajax。但是我不明白如何通过编程将状态从0更改为1。

我在控制器中的代码-

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Controller\ControllerBase;

class CustomAjaxLinkController extends ControllerBase{

  public function customAjaxLinkAlert($node) {

    $query = \Drupal::entityQuery('node');
    $query->condition('status',1);
    $node = $query->execute();
    $status =  $node-> isPublished();
    if ($status === TRUE) {
      $node->status = 0;
        $node->save();
    }

    $response = new AjaxResponse();

    $selector = '.customAjaxLinkAlert' ;
    $response->addCommand(new RemoveCommand($selector,$node));
    return $response;
  }
}      
                                                                                                                                    There is an error in it,but I cannot figure out what I am doing wrong

解决方法

您可以在Drupal控制器中通过ajax过去数据

         $.ajax({
            type: 'POST',url: '/your_module/your_function',data: {'data': data},});

然后在您的控制器中

<?php

namespace Drupal\your_module\your_contoller;

use Drupal\Core\Controller\ControllerBase;

/**
 * Class your_contoller.
 */
class your_contoller extends ControllerBase {

  public function your_function() {
    do what you want
  }

}

OR

您的按钮在表单内,并将提交值。