Commit 4d7e427a authored by Marty Landman's avatar Marty Landman
Browse files

First release for Drupal 8 of Personal Notes contrib module

parents
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
/**
 * @file
 * Styling for the Personal Notes module theme.
 */

/**
 * Personal Notes custom block styling.
 */
 
#block-personal-notesblock {
}
 
#block-personal-notesblock h2 {
  font-size: larger;
  text-align: center;
}
 
#personalnotes-notes-list {
}

.personalnotes-note-title {
  text-align: left;
  margin: 1.5em 0 1em 1.75em;
  font-variant: all-small-caps;
}

.personalnotes-note-note {
  font-style: oblique;
}
+5 −0
Original line number Diff line number Diff line
name: Personal Notes
description: Allow registered users to create and delete notes to themselves which display on a block.
core: 8.x
package: Custom
type: module

personal_notes.install

0 → 100644
+74 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Install, update and uninstall functions for the Personal Notes module.
 */

/**
 * Implements hook_install().
 *
 * Creates some default entries on this module custom table.
 *
 * @see hook_install()
 */
function personal_notes_install() {				//	create a note for the administrator as a demonstration
  $fields = [
    'uid' => '1',
    'title' => t('Title One'),
    'note' => t('Here is the first note created for Personal Notes belonging to user 1'),
    'created' => \Drupal::time()->getCurrentTime(),
  ];
  db_insert('personal_notes_notes')
    ->fields($fields)
    ->execute();
}

/**
 * Implements hook_schema().
 *
 * Defines the database tables used by this module.
 *
 * @see hook_schema()
 */
function personal_notes_schema() {
  $schema['personal_notes_notes'] = [
    'description' => t('Stores notes that users write.'),
    'fields' => [
      'uid' => [
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {user}.uid of the user who created the note.')
      ],
      'notenum' => [
        'description' => t('The primary identifier for a personal note.'),
		  'type' => 'serial',
		  'unsigned' => TRUE,
        'not null' => TRUE
      ],
      'title' => [
        'description' => t('The title of the note.'),
        'type' => 'varchar',
        'not null' => TRUE,
        'length' => 24
      ],
      'note' => [
        'description' => t('The text of the note.'),
        'type' => 'varchar',
        'not null' => TRUE,
        'length' => 512
      ],
      'created' => [
        'description' => t('A Unix timestamp indicating when the note was created.'),
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0
      ],
    ],
    'primary key' => ['notenum'],
  ];

  return $schema;
}
+6 −0
Original line number Diff line number Diff line
#	CSS file for Personal Notes theming
personal_notes:
  version: 1.x
  css:
    theme:
      css/personal-notes.theme.css: {}

personal_notes.module

0 → 100644
+98 −0
Original line number Diff line number Diff line
<?php
/*
 * @file
 * code for personal_notes module
*/

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function personal_notes_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.personal_notes':
      $output = '';
      $variables = [
        ':addnote' => Url::fromRoute('personalnote.create.form')->toString(),
        ':dltnote' => Url::fromRoute('personalnote.delete.form')->toString(),
        ':mainmnu' => Url::fromUri('internal:/admin/structure/menu/manage/account')->toString(),
        ':assignblock' => Url::fromRoute('block.admin_display')->toString(),
      ];
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Personal notes consist of titles and text rendered into a block. The notes are ' . 
                 'specific to each authenticated user on the site and can be added and deleted.') . '</p>';
      $output .= '<h3>' . t('Configuring Personal Notes') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Creating Links to Add and Delete Personal Notes') . '</dt>';
      $output .= '<dd>' . t('Permitted users add and delete their own Personal Notes by going to the ' .
                 'links <a href="' . $variables[':addnote'] . '">Add a Personal Note</a> and <a href="' .
                 $variables[':dltnote'] . '">Delete Personal Note(s)</a> ' . 
                 'respectively. We recommend adding these links to your site&apos;s <a href="' . 
                 $variables[':mainmnu'] . '">User account menu</a>, but of course they can go anywhere you want.') . '</dd>';
      $output .= '<dt>' . t('Displaying Personal Notes in a Block') . '</dt>';
      $output .= '<dd>' . t('In order for users to see their Personal Notes you&apos;ll have to <a href="' . 
                 $variables[':assignblock'] . '">assign the Personal Notes block to a region</a> on your site.') . '</dd>';
      $output .= '</dl>';

      return $output;
  }
}

/**
 * Implements hook_preprocess_HOOK() for the block template.
 */
function personal_notes_preprocess_block(&$vars) {
  if($vars['plugin_id'] == 'block_personal_notesblk') {
    $vars['#cache']['max-age'] = 0;								//	This stops the block from being cached.  Otherwise the 
  }																		//	personal notes won't update in the display consistently.
}

/**
 * Retrieve the notes made by the requesting user.
 *
 * @return
 * Result of DB query.
 */
function _personal_notes_fetch_content_db() {
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  $uid= $user->get('uid')->value;
  $query = \Drupal::database()->select('personal_notes_notes', 'r')
    ->fields('r', array('title','note','notenum'))
    ->condition('uid', $uid, '=')
    ->orderBy('notenum', 'ASC');
  $results = $query->execute()->fetchAll();
  return $results;
}

/**
 * Implements hook_theme().
 */
function personal_notes_theme($existing, $type, $theme, $path) {
  return [
    'block--personal_notes' => [
      'variables' => [
        'notes' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function personal_notes_form_personal_notes_add_content_alter(&$form, $form_state, $form_id) {
  $form['#validate'][] = '_personal_note_add_validate';
}

/**
 * Custom validation for the add note form.
 */
function _personal_note_add_validate(&$form, $form_state) {
  if (strlen( $form_state->getValue('note_content') ) > 500) {
    $form_state->setErrorByName('note_content', t('The note is too long, keep it under 500 characters.'));
  }
}