每周节点限制在Drupal 8个用户组

问题描述 投票:0回答:1

我一直在努力做到这一点:

极限验证的用户(每个人)发布每周只到3篇文章。如果他们已经发布的3篇文章,本周将有错误消息显示,他们将不能够访问节点/添加/文章页面。

我试图按照this(使用规则和标志),但他们使用“规则,每天一次”模块巫不适用于D8做到每天。

我看到Node Limit模块,但它崩溃上安装后,我的D8。

如何任何的指导和帮助,以解决此问题?

编辑

您可以在这里找到(Github Link)我从所选答案的帮助下做出的解决方案。

drupal nodes limit drupal-8 user-accounts
1个回答
1
投票

我认为,简单的办法就是自定义验证添加到节点形式:当用户试图提交一个新的节点(页,文章......),检查它们是否贴到这个星期3篇文章,如果是的话 - >停止从形式提交数据,如果没有 - >保存新的节点。

这里是我的代码解决方案,它应该放在您的自定义模块:

- 实施hook_form_alter添加自定义的验证

function MY_MODULE_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // check if user is about to create new node of type page
  // This custom validation won't be called on EDIT form (edit form_id is node_page_edit_form)
  if($form_id == 'node_page_form') {
    $form['#validate'][] = '_node_page_form_custom_validate';
  }
}

- 在自定义验证功能,检查当前用户张贴到本周3篇文章

function _node_page_form_custom_validate(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  // 1. get current user id
  $user_uid = \Drupal::currentUser()->id();

  // 2. count number of nodes created by current user in last week
  $query = \Drupal::entityQuery('node');
  $query->condition('type', array('page', 'article'), 'IN'); // Limit the type of node to check
  $query->condition('uid', $user_uid);

  $first_day_of_week = strtotime('Last Monday'); // choose the day you define as First day of week
  $last_day_of_week = strtotime('Next Monday');
  $query->condition('created', array($first_day_of_week, $last_day_of_week), 'BETWEEN');

  $count = $query->count()->execute();

  // 3. if number of posts reachs limit, stop the form from saving data
  if($count >= 3) {
    $form_state->setErrorByName('', t('You reached the limit of @count pages/articles this week', array('@count' => $count)));
  }
}

希望这有助于。

© www.soinside.com 2019 - 2024. All rights reserved.