diff --git a/img/publish-sheet.png b/img/publish-sheet.png new file mode 100644 index 0000000..30a955e Binary files /dev/null and b/img/publish-sheet.png differ diff --git a/modules/gss/src/Plugin/Shortcode/GssShortcode.php b/modules/gss/src/Plugin/Shortcode/GssShortcode.php index 6f607b4..07a9c94 100644 --- a/modules/gss/src/Plugin/Shortcode/GssShortcode.php +++ b/modules/gss/src/Plugin/Shortcode/GssShortcode.php @@ -90,8 +90,16 @@ public function process(array $attributes, $text, $langcode = Language::LANGCODE } // Adds class to add search. $gss_plain = $search ? "
" . $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 ''; diff --git a/shentity.deploy.php b/shentity.deploy.php new file mode 100644 index 0000000..2bd35e3 --- /dev/null +++ b/shentity.deploy.php @@ -0,0 +1,47 @@ +setLabel(t('Full share CSV url')) + ->setDescription(t('Newer sheets changed the url. Grab the full csv url and place it here')) + ->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"; + $entity->set('full_key', $full_key); + $entity->save(); + } +} diff --git a/shentity.module b/shentity.module index b584687..661771a 100644 --- a/shentity.module +++ b/shentity.module @@ -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(). */ diff --git a/src/Entity/Shentity.php b/src/Entity/Shentity.php index 4d723c8..8f036c4 100644 --- a/src/Entity/Shentity.php +++ b/src/Entity/Shentity.php @@ -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.') . + " See screenshot") ->setSettings([ 'max_length' => 255, 'text_processing' => 0, @@ -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', diff --git a/src/Plugin/PullGoogleSheet.php b/src/Plugin/PullGoogleSheet.php index b82b587..97792a9 100644 --- a/src/Plugin/PullGoogleSheet.php +++ b/src/Plugin/PullGoogleSheet.php @@ -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; @@ -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']; } @@ -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'], ], ];