Skip to content

Commit

Permalink
BlogHelper getTitle()、getBlogName()、getDescription()などの動作が正しくない場合がある fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 16, 2023
1 parent 7cc2a7b commit ec6efca
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
13 changes: 13 additions & 0 deletions plugins/bc-blog/src/Service/BlogContentsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,19 @@ public function getContentsTemplateRelativePath(array $options): string
return 'BcBlog...' . DS . 'Blog' . DS . $options['contentsTemplate'] . DS . $options['template'];
}

/**
* コンテンツ名よりブログコンテンツを取得する
*
* Contents を含む
*
* @param string $name
* @return array|EntityInterface|null
*/
public function findByName(string $name)
{
return $this->BlogContents->find()->where(['Contents.name' => $name])->contain('Contents')->first();
}

/**
* 検索インデックスの再構築が必要か判定
*
Expand Down
40 changes: 29 additions & 11 deletions plugins/bc-blog/src/View/Helper/BlogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ public function blogName()
*
* @return string
* @checked
* @noTodo
*/
public function getBlogName()
{
// TODO $this->currentContent に変更する
return $this->_View->getRequest()->getAttribute('currentContent')->name;
return $this->currentContent->name;
}

/**
Expand All @@ -250,11 +250,11 @@ public function title()
*
* @return string
* @checked
* @noTodo
*/
public function getTitle()
{
// TODO $this->currentContent に変更する
return $this->_View->getRequest()->getAttribute('currentContent')->title;
return $this->currentContent->title;
}

/**
Expand Down Expand Up @@ -992,7 +992,7 @@ public function getPostImg($post, $options = [])
}
$img = $this->BcBaser->getImg($url, $options);
if ($link) {
return $this->BcBaser->getLink($img, $this->_View->getRequest()->getAttribute('currentContent')->url . 'archives/' . $post->no);
return $this->BcBaser->getLink($img, $this->currentContent->url . 'archives/' . $post->no);
} else {
return $img;
}
Expand Down Expand Up @@ -1684,7 +1684,24 @@ public function posts($contentsName = [], $num = 5, $options = [])
} else {
$data = ['posts' => $blogPosts];
}

if(is_array($contentsName)) {
$blogContent = $blogContentsService->findByName($contentsName[0]);
} else {
$blogContent = $blogContentsService->findByName($contentsName);
}

$currentBlogContentId = null;
if($this->currentBlogContent) {
$currentBlogContentId = $this->currentBlogContent->id;
}

$this->setContent($blogContent->id);
$this->BcBaser->element($template, $data);

if($currentBlogContentId) {
$this->setContent($currentBlogContentId);
}
}

/**
Expand Down Expand Up @@ -1741,7 +1758,7 @@ public function parseContentName($contentsName, $options)
}
}
if ($options['autoSetCurrentBlog'] && empty($options['contentUrl']) && empty($options['contentId'])) {
$currentContent = $this->_View->getRequest()->getAttribute('currentContent');
$currentContent = $this->currentContent;
if ($this->isBlog() && !empty($currentContent->entity_id)) {
$options['contentId'] = $currentContent->entity_id;
}
Expand Down Expand Up @@ -1878,11 +1895,11 @@ private function _mergePostCountToBlogsData(array $blogsData)
*
* @return bool
* @checked
* @noTodo
*/
public function isBlog()
{
// TODO $this->currentContent に変更
return (!empty($this->_View->getRequest()->getAttribute('currentContent')->plugin) && $this->_View->getRequest()->getAttribute('currentContent')->plugin == 'BcBlog');
return (!empty($this->currentContent->plugin) && $this->currentContent->plugin == 'BcBlog');
}

/**
Expand All @@ -1909,6 +1926,7 @@ public function getContentsUrl(int $blogContentId, $base = true)
* @param int $blogContentId ブログコンテンツID
* @return bool
* @checked
* @noTodo
*/
public function isSameSiteBlogContent($blogContentId)
{
Expand All @@ -1919,9 +1937,9 @@ public function isSameSiteBlogContent($blogContentId)
])->first();
$siteId = $content->site_id;
$currentSiteId = 0;
// TODO $this->currentContent に変更
if (!empty($this->_View->getRequest()->getAttribute('currentContent')->alias_id)) {
$content = $contentsTable->get($this->_View->getRequest()->getAttribute('currentContent')->alias_id);

if (!empty($this->currentContent->alias_id)) {
$content = $contentsTable->get($this->currentContent->alias_id);
$currentSiteId = $content->site_id;
} elseif ($this->_View->getRequest()->getAttribute('currentSite')->id) {
$currentSiteId = $this->_View->getRequest()->getAttribute('currentSite')->id;
Expand Down
13 changes: 13 additions & 0 deletions plugins/bc-blog/tests/TestCase/Service/BlogContentsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,17 @@ public function test_getContentsTemplateRelativePath()
$this->assertEquals($rs, 'BcBlog.../Blog/default/posts');

}

/**
* test findByName
* @return void
*/
public function testFindByName()
{
$this->loadFixtureScenario(BlogContentScenario::class, 1, 1, null, 'test', '/test');
$blogContent = $this->BlogContentsService->findByName('test');
$this->assertEquals($blogContent->id, 1);
$this->assertEquals($blogContent->content->url, '/test');
$this->assertNull($this->BlogContentsService->findByName('non'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ public function getTagLinkDataProvider()
* @param expected string 期待値
* @param message string テスト失敗時に表示されるメッセージ
* @dataProvider postsDataProvider
* @todo $this->currentContent が初期状態で固定ページになっている場合に正常に動作するテストを追加する
*/
public function testPosts($currentUrl, $contentsName, $num, $options, $expected, $message = null)
{
Expand Down

0 comments on commit ec6efca

Please sign in to comment.