Search documentation of phoenix_kit

# PhoenixKit.Sitemap.Sources.Source behaviour (phoenix_kit v1.6.19)

View Source

Behaviour for sitemap data sources.

Each source module must implement this behaviour to provide URL entries for sitemap generation. Sources can collect data from various PhoenixKit modules like Entities, Blogging, Pages, etc.

## Implementing a Source

    defmodule PhoenixKit.Sitemap.Sources.MyModule do
      @behaviour PhoenixKit.Sitemap.Sources.Source

      alias PhoenixKit.Sitemap.UrlEntry

      @impl true
      def source_name, do: :my_module

      @impl true
      def enabled?, do: MyModule.enabled?()

      @impl true
      def collect(_opts) do
        if enabled?() do
          # Collect URLs from your module
          [
            UrlEntry.new(%{
              loc: "https://example.com/my-page",
              lastmod: DateTime.utc_now(),
              changefreq: "weekly",
              priority: 0.8,
              title: "My Page",
              category: "My Module",
              source: :my_module
            })
          ]
        else
          []
        end
      end
    end

## Adding Source to Generator

After implementing the source, add it to the generator's source list in `PhoenixKit.Sitemap.Generator.collect_all_entries/2`.

# Summary

## Callbacks

collect(opts)

Collects all URL entries from this source.

enabled?()

Checks if this source is enabled and should be included in sitemap.

source_name()

Returns the unique name/identifier for this source.

## Functions

safe_collect(source_module, opts \\ [])

Safely collects entries from a source, handling errors gracefully.

valid_source?(module)

Helper function to check if a source module is valid.

# Callbacks

# collect(opts)

    @callback collect(opts :: keyword()) :: [PhoenixKit.Sitemap.UrlEntry.t()]

Collects all URL entries from this source.

Options may include:

- `:language` - Preferred language for content
- `:base_url` - Base URL for building full URLs

Should return an empty list if the source is disabled.

# enabled?()

    @callback enabled?() :: boolean()

Checks if this source is enabled and should be included in sitemap.

Should return false if the underlying module is disabled or if sitemap collection is turned off for this source.

# source_name()

    @callback source_name() :: atom()

Returns the unique name/identifier for this source.

Used for logging, filtering, and organizing sitemap entries.

# Functions

# safe_collect(source_module, opts \\ [])

    @spec safe_collect(
      module(),
      keyword()
    ) :: [PhoenixKit.Sitemap.UrlEntry.t()]

Safely collects entries from a source, handling errors gracefully.

# valid_source?(module)

    @spec valid_source?(module()) :: boolean()

Helper function to check if a source module is valid.

Ensures the module is loaded before checking for exported functions.