【问题标题】:Adding a "news item" style thumbnail image to a Dexterity content type将“新闻项目”样式的缩略图添加到 Dexterity 内容类型
【发布时间】:2014-03-28 17:59:43
【问题描述】:

我正在为自定义 Dexterity 内容类型制作列表页面,我想提取其中一张图片以显示在列表中,就像一个充满新闻项目的文件夹的列表页面显示的那样标题和描述旁边的缩略图(即 folder_summary_view)。

解决此问题的最佳方法是什么?我尝试自定义 folder_summary_view 模板以更改缩略图代码以查找 'item_object/thumbnail' 但它返回错误,因为它是 blob 图像或其他东西:

<a href="#" tal:condition="exists:item_object/thumbnail" tal:attributes="href python:test(item_type in use_view_action, item_url+'/view', item_url)">
<img src="" alt="" tal:replace="structure python: path('nocall:item_object/thumbnail')(scale='original', css_class='tileImage')" />
</a>

返回:

    Module Products.PageTemplates.ZRPythonExpr, line 48, in __call__
    __traceback_info__: path('nocall:item_object/thumbnail')(scale='original', css_class='tileImage')
    Module PythonExpr, line 1, in <expression>

TypeError: 'NamedBlobImage' object is not callable 

我想我也有兴趣了解如何调用任何其他字段。它会自动提取标题和描述。有没有一种简单的方法来调用 moar 领域?即,如果有一个名为:developer_name 的文本字段 - 我将如何在文件夹摘要视图中的标题之后显示它?

我看到this question 以防万一,但它似乎与迁移更相关,而不是显示内容。

【问题讨论】:

  • 也感谢您用 Martijn P 的回答指出参考任务,它像往常一样全面、完整并深入地解释它。
  • 这个问题的答案很好!我想添加 plone.app.contenttypes,它为 Plone 5 提供基本内容类型,具有“LeadImage”行为,允许任何 Dexterity 内容类型具有像新闻项目一样的主要图像。实现细节github.com/plone/plone.app.contenttypes/blob/master/plone/app/…

标签: plone dexterity


【解决方案1】:

folder_summary_view 仍然需要为 Plone 的默认新闻项目更新,如下所示(适用于两者;Plone 的默认 Archetype- 和 Dexterity-newsitem):

            <a href="#"
               tal:condition="exists:item_object/@@images/image"
               tal:attributes="href python:test(item_type in use_view_action, item_url+'/view', item_url)">
                <img src="" alt=""
                     tal:replace="structure item_object/@@images/image/mini" />
            </a>

“image”是字段名称,以防您的不同。

完整参考请参阅 plone.app.imaging 的 README

我希望很快能找到一些时间来提交,除非其他人正在这样做;)

注意:我想我记得不建议使用python:test(),也许那部分也应该调整。

对于一般渲染你的领域,你可以使用 good ol':

    <div tal:content="structure context/developer_name" />

使用 TTW 创建的 Dexterity-CT 和文本字段进行测试。

另见(有一个令人深思的例子): http://developer.plone.org/reference_manuals/external/plone.app.dexterity/custom-views.html#simple-views

【讨论】:

  • 非常欢迎您,感谢您将 Emerald-sprint 的视频展示放到网上,这样人们就可以看到成为 Plone-world 的一部分是多么酷 :) 我希望您能现在也可以获取任意字段名。顺便说一句,Hector 的答案基本上是一个长格式符号(如果您测试过:它是否适用于 Dex?),另请参阅 p.a.imaging 文档。
【解决方案2】:

如果您仔细查看folder_summary_view template,您会发现您需要做的就是为您的类添加一些辅助方法:

<a href="#"
   tal:condition="exists:item_object/image_thumb"
   tal:attributes="href python:test(item_type in use_view_action, item_url+'/view', item_url)">
    <img src="" alt=""
         tal:replace="structure python: path('nocall:item_object/tag')(scale='thumb', css_class='tileImage')" />
</a>

we did something similar in collective.nitf 通过创建 getImageimageCaptiontagimage_thumb 函数(你可能会赢)不需要全部)。

另外,请注意将映射 getImage 函数的 image 属性。

class NITF(Container):
    implements(INITF)
    ...    
    # The purpose of these methods is to emulate those on News Item
    def getImage(self):
        """Return the first Image inside the News Article."""
        content_filter = {'portal_type': 'Image'}
        images = self.listFolderContents(content_filter)
        return images[0] if len(images) > 0 else None

    image = getImage # XXX: a hack to support summary_view

    def imageCaption(self):
        image = self.getImage()
        if image is not None:
            return image.Description()

    def tag(self, **kwargs):
        # tag original implementation returns object title in both, alt and
        # title attributes
        image = self.getImage()
        if image is not None:
            scales = image.restrictedTraverse('@@images')
            if 'scale' in kwargs:
                scale_id = kwargs.get('scale')
                del kwargs['scale']
            else:
                scale_id = 'thumb'
            kwargs['alt'] = image.Description()
            kwargs['title'] = image.Title()
            scale = scales.scale(fieldname='image', scale=scale_id)
            return scale.tag(**kwargs)

    def image_thumb(self):
        """Return a thumbnail."""
        image = self.getImage()
        if image is not None:
            view = image.unrestrictedTraverse('@@images')
            # Return the data
            return view.scale(fieldname='image', scale='thumb').data

看看那个代码。

【讨论】:

    猜你喜欢
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    相关资源
    最近更新 更多