Class: Jekyll::ContentfulEntryPageGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-contentful.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/jekyll-contentful.rb', line 41

def generate(site)

  if site.config['contentful']['preview']
    api_url = 'preview.contentful.com'
    access_token =  site.config['contentful']['preview_access_token']
  else 
    api_url = 'cdn.contentful.com'
    access_token =  site.config['contentful']['production_access_token']
  end

  client = ::Contentful::Client.new(
    access_token: access_token,
    space: site.config['contentful']['space'],
    api_url: api_url
  )

  # Loop over all content types
  site.config['contentful']['content_types'].each do |content_type_id|
    # Get name for content type ID
    content_type = client.content_types('sys.id' => content_type_id).first

    throw "Content_type \'#{content_type_id}\' does not exist." if content_type.nil?

    localization = site.config['contentful']['localization'] || [{locale: nil, url_prefix: ""}]      
    
    # Get all entries of content type

    localization.each do |loc|
      entries = client.entries(content_type: content_type_id, locale: loc["locale"], limit: 1000)
      entries.each do |entry|

        next if entry.fields.nil?

        published_locales_field = site.config['contentful']['published_locales_field']
        pub_langs = published_locales_field.nil? ? nil : entry.fields[published_locales_field.to_sym]

        if pub_langs.nil? or pub_langs.map{|x| x.fields[:locale]}.include?(loc["locale"])
          site.pages << ContentfulEntryPage.new(site, entry, content_type.name, "#{loc['url_prefix']}")
        end
      end
    end

  end
end