Initial commit of archive_tree module

This commit is contained in:
2026-01-23 21:52:25 -06:00
commit c16a1e6d7e
8 changed files with 613 additions and 0 deletions

6
archive_tree.info.yml Normal file
View File

@@ -0,0 +1,6 @@
name: Archive tree
machine_name: archive_tree
type: module
description: 'Provides an Archive tree block.'
core_version_requirement: ^10 || ^11
package: Custom

View File

@@ -0,0 +1,5 @@
archive_tree_styles:
version: 1.x
css:
theme:
css/archive_tree.css: {}

2
archive_tree.module Normal file
View File

@@ -0,0 +1,2 @@
<?php
// Archive tree module file. Required for Drupal module recognition.

View File

@@ -0,0 +1,239 @@
langcode: en
status: true
dependencies:
module:
- node
id: archive_by_month
label: 'Archive by month'
module: views
description: 'Lists all content for a given year and month.'
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
title: 'Archive by month'
fields:
title:
id: title
table: node_field_data
field: title
relationship: none
group_type: group
entity_type: node
entity_field: title
plugin_id: field
label: ''
exclude: false
alter:
alter_text: false
make_link: true
absolute: false
word_boundary: false
ellipsis: false
strip_tags: false
trim: false
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
pager:
type: mini
options:
offset: 0
pagination_heading_level: h4
items_per_page: 10
total_pages: null
id: 0
tags:
next: ''
previous: ''
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
empty: { }
sorts:
created:
id: created
table: node_field_data
field: created
relationship: none
group_type: group
entity_type: node
entity_field: created
plugin_id: date
order: DESC
granularity: second
arguments:
created_year:
id: created_year
table: node_field_data
field: created_year
relationship: none
group_type: group
entity_type: node
plugin_id: date_year
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
created_month:
id: created_month
table: node_field_data
field: created_month
relationship: none
group_type: group
entity_type: node
plugin_id: date_month
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
filters:
status:
id: status
table: node_field_data
field: status
entity_type: node
entity_field: status
plugin_id: boolean
value: '1'
group: 1
expose:
operator: ''
style:
type: default
row:
type: 'entity:node'
options:
view_mode: teaser
query:
type: views_query
options:
query_comment: ''
disable_sql_rewrite: false
distinct: false
replica: false
query_tags: { }
relationships: { }
header: { }
footer: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
page_1:
id: page_1
display_title: Page
display_plugin: page
position: 1
display_options:
display_extenders: { }
path: archive-tree/%/%
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }

View File

@@ -0,0 +1,67 @@
langcode: en
status: true
dependencies:
module:
- node
id: archive_by_year
label: 'Archive by year'
description: 'Lists all content for a given year.'
module: views
display:
page_1:
display_plugin: page
id: page_1
display_title: 'Archive by year'
position: 0
display_options:
path: 'archive-tree/%'
arguments:
created_year:
id: created_year
table: node_field_data
field: created
default_action: empty
exception: ''
title_enable: true
title: 'Archive for year %1'
default_argument_type: fixed
default_argument_options:
argument: ''
summary:
sort_order: asc
number_of_records: 0
format: default_summary
base_path: ''
count: true
limit: 0
plugin_id: date
date_format: 'Y'
granularity: year
filters:
status:
id: status
table: node_field_data
field: status
value: '1'
provider: node
fields:
title:
id: title
table: node_field_data
field: title
label: 'Title'
alter:
make_link: true
path: 'node/[nid]'
sort:
created:
id: created
table: node_field_data
field: created
order: DESC
pager:
type: some
options:
items_per_page: 20
style:
type: default

View File

@@ -0,0 +1,210 @@
langcode: en
status: true
dependencies:
config:
- core.entity_view_mode.node.teaser
module:
- node
- user
id: archive_by_year
label: 'Archive by year'
module: views
description: 'Lists all content for a given year.'
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
id: default
display_title: Default
display_plugin: default
position: 0
display_options:
title: 'Archive by year'
fields:
title:
id: title
table: node_field_data
field: title
relationship: none
group_type: group
entity_type: node
entity_field: title
plugin_id: field
label: ''
exclude: false
alter:
alter_text: false
make_link: true
absolute: false
word_boundary: false
ellipsis: false
strip_tags: false
trim: false
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: string
settings:
link_to_entity: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
pager:
type: mini
options:
offset: 0
pagination_heading_level: h4
items_per_page: 10
total_pages: null
id: 0
tags:
next: ''
previous: ''
expose:
items_per_page: false
items_per_page_label: 'Items per page'
items_per_page_options: '5, 10, 25, 50'
items_per_page_options_all: false
items_per_page_options_all_label: '- All -'
offset: false
offset_label: Offset
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
empty: { }
sorts:
created:
id: created
table: node_field_data
field: created
relationship: none
group_type: group
entity_type: node
entity_field: created
plugin_id: date
order: DESC
granularity: second
arguments:
created_year:
id: created_year
table: node_field_data
field: created_year
relationship: none
group_type: group
entity_type: node
plugin_id: date_year
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
summary_options:
base_path: ''
count: true
override: false
items_per_page: 25
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
filters:
status:
id: status
table: node_field_data
field: status
entity_type: node
entity_field: status
plugin_id: boolean
value: '1'
group: 1
expose:
operator: ''
style:
type: default
row:
type: 'entity:node'
options:
view_mode: teaser
query:
type: views_query
options:
query_comment: ''
disable_sql_rewrite: false
distinct: false
replica: false
query_tags: { }
relationships: { }
header: { }
footer: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }
page_1:
id: page_1
display_title: Page
display_plugin: page
position: 1
display_options:
display_extenders: { }
path: archive-tree/%
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }

9
css/archive_tree.css Normal file
View File

@@ -0,0 +1,9 @@
.archive-tree-year {
font-weight: bold;
}
.archive-tree-month {
margin-left: 2em;
padding-left: 0.5em;
border-left: 2px solid #ccc;
display: block;
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Drupal\archive_tree\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides an 'Archive tree' block.
*
* @Block(
* id = "archive_tree_block",
* admin_label = @Translation("Archive tree"),
* )
*/
class ArchiveTreeBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $storage->getQuery()
->condition('type', 'article')
->condition('status', 1)
->sort('created', 'DESC')
->accessCheck(TRUE)
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
$nids = $query->execute();
$nodes = $storage->loadMultiple($nids);
$tree = [];
foreach ($nodes as $node) {
$created = $node->getCreatedTime();
$year = date('Y', $created);
$month = date('m', $created);
if (!isset($tree[$year])) {
$tree[$year] = ['count' => 0, 'months' => []];
}
if (!isset($tree[$year]['months'][$month])) {
$tree[$year]['months'][$month] = 0;
}
$tree[$year]['count']++;
$tree[$year]['months'][$month]++;
}
krsort($tree); // Descending years
foreach ($tree as &$data) {
krsort($data['months']); // Descending months
}
$output = '';
foreach ($tree as $year => $data) {
$year_url = '/archive-tree/' . $year;
$output .= '<details><summary class="archive-tree-year">';
$output .= '<a href="' . $year_url . '">' . $year . '</a> (' . $data['count'] . ')';
$output .= '</summary>';
foreach ($data['months'] as $month => $count) {
$month_url = '/archive-tree/' . $year . '/' . $month;
$output .= '<div class="archive-tree-month">';
$output .= '<a href="' . $month_url . '">' . $month . '</a> (' . $count . ')';
$output .= '</div>';
}
$output .= '</details>';
}
return [
'#markup' => $output,
'#allowed_tags' => ['details', 'summary', 'a', 'div'],
'#attached' => [
'library' => [
'archive_tree/archive_tree_styles',
],
],
];
}
}