mynotes-8.x-1.3/mynotes.module
mynotes.module
<?php
/**
* @file
* Contains mynotes.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
/**
* Implements hook_form_alter().
*
* Alter 'Notes' View exposed form.
*/
function mynotes_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-notes-notes-search') {
$form['keywords']['#size'] = '80%';
$form['keywords']['#attributes']['placeholder'] = t('What do you want to find?');
}
}
/**
* Implements hook_theme().
*
* Register a module theme implementations.
*/
function mynotes_theme($existing, $type, $theme, $path) {
return [
'node__note__teaser' => [
'template' => 'node--note--teaser',
'base hook' => 'node',
],
];
}
/**
* Implements hook_preprocess_HOOK().
*
* Pass variables to node note template.
* Attach mynotes library.
*/
function mynotes_preprocess_node__note(&$variables) {
$variables['star_image'] = drupal_get_path('module', 'mynotes') . '/images/star.png';
$variables['#attached']['library'][] = 'mynotes/mynotes';
}
/**
* Implements hook_entity_extra_field_info().
*
* Define pseudo-field, and add it to the note content type.
*/
function mynotes_entity_extra_field_info() {
$extra = [];
foreach (NodeType::loadMultiple() as $bundle) {
if ($bundle->get('type') != 'note') {
continue;
}
$extra['node'][$bundle->Id()]['display']['note_actions'] = [
'label' => t('Note actions'),
'weight' => 100,
'visible' => TRUE,
];
}
return $extra;
}
/**
* Implements hook_ENTITY_TYPE_view().
*
* Assemble content of the pseudo-field.
*/
function mynotes_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
// Pseudo-field render array.
if ($display->getComponent('note_actions')) {
$build['note_actions'] = [
'#type' => 'dropbutton',
'#links' => [
'star' => [
'title' => $entity->field_stared->value ? t('Remove star') : t('Add star'),
'url' => Url::fromRoute('mynotes.action', [
'fieldname' => 'stared',
'nid' => $entity->nid->value,
]),
],
'archive' => [
'title' => $entity->field_archived->value ? t('Unarchive') : t('Archive'),
'url' => Url::fromRoute('mynotes.action', [
'fieldname' => 'archived',
'nid' => $entity->nid->value,
]),
],
'edit' => [
'title' => t('Edit note'),
'url' => Url::fromRoute('entity.node.edit_form', [
'node' => $entity->nid->value,
'destination' => Url::fromRoute('view.notes.notes_search')
->toString(),
]),
],
'delete' => [
'title' => t('Delete note'),
'url' => Url::fromRoute('entity.node.delete_form', [
'node' => $entity->nid->value,
'destination' => Url::fromRoute('view.notes.notes_search')
->toString(),
]),
],
],
];
}
}
/**
* Implements hook_node_access().
*
* Limit access to notes.
*/
function mynotes_node_access(NodeInterface $node, $op, AccountInterface $account) {
$type = $node->bundle();
$has_permission = $account->hasPermission('access mynotes');
if ($type != 'note' || $has_permission) {
return AccessResult::neutral();
}
return AccessResult::forbidden();
}
/**
* Implements hook_form_FORM_ID_alter().
*
* This form alter is required to make Facets and exposed filters work together.
*/
function mynotes_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$facet_params = \Drupal::request()->query->filter('f');
if (!empty($facet_params) && $form['#id'] == 'views-exposed-form-notes-notes-search') {
foreach ($facet_params as $delta => $value) {
$form['f[' . $delta . ']'] = [
'#type' => 'hidden',
'#value' => Html::escape($value),
];
}
}
}
