app/Customize/Controller/Block/TopNewsController.php line 50

Open in your IDE?
  1. <?php
  2. namespace Customize\Controller\Block;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Repository\NewsRepository;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. class TopNewsController extends AbstractController {
  9.   /**
  10.    * @var NewsRepository
  11.    */
  12.   protected $newsRepository;
  13.   /**
  14.    * NewsController constructor.
  15.    *
  16.    * @param NewsRepository $newsRepository
  17.    */
  18.   public function __construct(NewsRepository $newsRepository) {
  19.     $this->newsRepository $newsRepository;
  20.   }
  21.   private $_htImg;
  22.   /**
  23.    * @Route("/block/top_news", name="block_top_news")
  24.    * @Template("Block/top_news.twig")
  25.    */
  26.   public function index(Request $request) {
  27.     $rows1 $this->newsRepository->getList("1",4);
  28.     $rows2 $this->newsRepository->getList("2",4);
  29.     $this->_getHashImg($rows1);
  30.     $this->_getHashImg($rows2);
  31.     return [
  32.       'rows1' => $rows1,
  33.       'rows2' => $rows2,
  34.       'htImg' => $this->_htImg,
  35.     ];
  36.   }
  37.   /**
  38.    * 画像ハッシュ一覧
  39.    * @param array $rows
  40.    * @return hash
  41.    */
  42.   private function _getHashImg($rows){
  43.     if(!empty($rows)){
  44.       foreach($rows as $row){
  45.         $id $row->getId();
  46.         $pattern '/<img.*?src\s*=\s*[\"|\'](.*?)[\"|\'].*?>/i';
  47.         $images = array();
  48.         preg_match$pattern$row->getDescription(), $images );
  49.         $this->_htImg[$id] = empty($images[1]) ? "" $images[1];
  50.       }
  51.     }
  52.   }
  53. }