Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added img/publish-sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions modules/gss/src/Plugin/Shortcode/GssShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ public function process(array $attributes, $text, $langcode = Language::LANGCODE
}
// Adds class to add search.
$gss_plain = $search ? "<div class='table-search'></div>" . $entity->sheet->value : $entity->sheet->value;
$gss = check_markup($gss_plain, 'rich_text');
return $gss;

// Render the table.
$gss = [
'#type' => 'inline_template',
'#template' => '{{ gss_plain|raw }}',
'#context' => [
'gss_plain' => $gss_plain,
],
];
return $this->renderer->render($gss);
}
else {
return '';
Expand Down
47 changes: 47 additions & 0 deletions shentity.deploy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @file
* Deploy hooks for shentity.
*/

use Drupal\Core\Field\BaseFieldDefinition;

/**
* Add full_key field to shentity entity.
*/
function shentity_deploy_10000_fullcsv() {
$entity_type_manager = \Drupal::entityTypeManager();
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();

// Get the field storage definition for full_key.
$field_storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Full share CSV url'))
->setDescription(t('Newer sheets changed the url. Grab the full csv url and place it here'))
Comment thread
protitude marked this conversation as resolved.
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
->setDefaultValue(NULL)
->setRequired(FALSE);

// Install the new field.
$entity_definition_update_manager->installFieldStorageDefinition('full_key', 'shentity', 'shentity', $field_storage_definition);

// Entity query 'shentity' to get all entities of type 'shentity' with a null
// 'full_key'.
$query = $entity_type_manager->getStorage('shentity')->getQuery();
$query->condition('full_key', NULL, 'IS NULL');
$query->accessCheck(FALSE);
$result = $query->execute();

// Update each entity with the new full_key value.
foreach ($result as $entity_id) {
$entity = $entity_type_manager->getStorage('shentity')->load($entity_id);
$key = $entity->get('key')->value;
$gid = $entity->get('sheet_number')->value;
$full_key = "https://docs.google.com/spreadsheets/d/$key/pub?gid=$gid&single=true&output=csv";
Comment thread
protitude marked this conversation as resolved.
$entity->set('full_key', $full_key);
$entity->save();
}
}
27 changes: 24 additions & 3 deletions shentity.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,45 @@
use Drupal\Core\Cache\Cache;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;

/**
* Implements hook_entity_presave().
*/
function shentity_entity_presave(EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'shentity') {
$key = $entity->get('key')->value;
$key = $entity->get('full_key')->value;
$fields = $entity->get('field_row')->value;
$type = $entity->get('type')->value;
$sheet_number = $entity->get('sheet_number')->value;
$shift = $entity->get('shift')->value;
$google_sheet = \Drupal::service('shentity.pull.google.sheet');
$google_sheet->fetch($key, $fields, $type, $sheet_number, $shift);
$sheet_data = $google_sheet->getData()->__toString();

$google_sheet->fetch($key, $fields, $type, $sheet_number, $shift, TRUE);
$sheet_data = $google_sheet->getData();
if ($sheet_data instanceof \Stringable || $sheet_data instanceof \Drupal\Component\Render\MarkupInterface) {
$sheet_data = (string) $sheet_data;
}
$entity->set('sheet', $sheet_data);
}
}

/**
* Implements hook_form_alter().
*/
function shentity_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'shentity_form') {
// Old key field is hidden and not used anymore, but we want to keep it
// in the database for now.
if (isset($form['key'])) {
$form['key']['#access'] = FALSE;
}
if (isset($form['sheet_number'])) {
$form['sheet_number']['#access'] = FALSE;
}
}
}

/**
* Implements hook_cron().
*/
Expand Down
29 changes: 26 additions & 3 deletions src/Entity/Shentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,31 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDisplayConfigurable('view', TRUE);

$fields['key'] = BaseFieldDefinition::create('string')
->setLabel(t('Key'))
->setDescription(t('The key of the google sheet.'))
->setLabel(t('Key (Deprecated)'))
->setDescription(t('The key of the google sheet. Only use for old sheets.'))
->setSettings([
'max_length' => 255,
'text_processing' => 0,
])
// Set no default value.
->setDefaultValue(NULL)
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -6,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -6,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);

$fields['full_key'] = BaseFieldDefinition::create('string')
->setLabel(t('Full share CSV URL'))
->setDescription(t('Newer sheets changed the URL. Copy the full CSV URL and paste it here. Be sure to select the correct sheet (1), and comma seperated values (2) to create the proper URL to place here.') .
" <a href='/modules/custom/shentity/img/publish-sheet.png'>See screenshot</a>")
->setSettings([
'max_length' => 255,
'text_processing' => 0,
Expand Down Expand Up @@ -239,7 +262,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
])
// Set no default value.
->setDefaultValue(NULL)
->setRequired(TRUE)
->setRequired(FALSE)
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
Expand Down
35 changes: 28 additions & 7 deletions src/Plugin/PullGoogleSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\shentity\Plugin;

use Drupal\Component\Utility\Random;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Render\RendererInterface;
Expand Down Expand Up @@ -44,16 +45,35 @@ public function __construct(LoggerChannelFactoryInterface $channelFactory, Rende
/**
* Setup table or list from Google sheet.
*/
public function fetch($key, $fields, $type, $sheet_number, $shift) {
$key = !empty($key) ? Xss::filter($key) : NULL;
$gid = $sheet_number >= 0 ? Xss::filter($sheet_number) : NULL;
$shift = Xss::filter($shift);
public function fetch($key, $fields, $type, $sheet_number, $shift, $shentity = FALSE) {
if (!empty($key)) {
$parsed_url = parse_url($key);
if ($parsed_url !== FALSE
&& isset($parsed_url['scheme'], $parsed_url['host'], $parsed_url['path'])
&& $parsed_url['scheme'] === 'https'
&& $parsed_url['host'] === 'docs.google.com'
&& strpos($parsed_url['path'], '/spreadsheets/') === 0) {
// Valid Google Sheets URL.
}
else {
$key = NULL;
}
}
else {
$key = NULL;
}
$gid = ($sheet_number !== NULL && ctype_digit((string) $sheet_number)) ? Xss::filter((string) $sheet_number) : NULL;
$shift = ($shift !== NULL && ctype_digit((string) $shift)) ? Xss::filter((string) $shift) : NULL;

if ($key !== NULL && $gid !== NULL && $type == 'table') {
if ($key !== NULL && $type == 'table') {
$sheet_letters = $fields;
$table = new GoogleSheetsApi();
$table->sheetDefined($key, $sheet_letters, $gid, $shift);
$table->sheetDefined($key, $sheet_letters, $gid, $shift, $shentity);
$table_data = $table->getSheetData();
// Random characters for id.
$random = new Random();
$id = $random->string();
$id = 'shentity-' . preg_replace('/[^a-zA-Z0-9\-]/', '', substr($id, 0, 10));
if (isset($table_data['header'])) {
$table_header = $table_data['header'];
}
Expand All @@ -73,7 +93,8 @@ public function fetch($key, $fields, $type, $sheet_number, $shift) {
'#header' => $table_header,
'#rows' => $table_rows,
'#attributes' => [
'id' => 'gdoc-table',
'id' => $id,
'class' => ['shentity-table'],
],
];

Expand Down